dartuint8arrayuint8list

Why this custom implementation slower than the built-in?


May i ask, why this user-made implementations slower than built-in ones:

Sample:

English (Compulsory) -A
BSc 16 101
English
1
-3
Fundamentals of Mathematical Statistics
BSc 16 102
Maths
1
-3
Computer Fundamentals and Computing Software
BSc 16 103
CFCS
1
-3
Problem Solving Through C
BSc 16 104
PSC
1
-3
Lab based on "Computer Fundamentals and Computing Software"
BSc 16 105
CFCS Practice
-1
-3
Lab based on "Problem Solving Through C"
BSc 16 106
PSC Practice
-1
-3

Custom implementation, using Uint8List, and for loop to split the list of byte sequences:

print(DateTime.now().minute.toString()+':'+DateTime.now().second.toString()+':'+DateTime.now().millisecond.toString()+':'+DateTime.now().microsecond.toString());
  final Uint8List bytes = File("/tmp/sample.txt").readAsBytesSync();
  print(DateTime.now().minute.toString()+':'+DateTime.now().second.toString()+':'+DateTime.now().millisecond.toString()+':'+DateTime.now().microsecond.toString());
  List<List<int>> separated = List<List<int>>.empty(growable: true);
  separated.add(List<int>.empty(growable: true));
  for(int i = 0; i< bytes.length;i++){
    if(bytes[i] == 10){
      separated.add(List<int>.empty(growable: true));
    }else{
      separated.last.add(bytes[i]);
    }
  }
  print(DateTime.now().minute.toString()+':'+DateTime.now().second.toString()+':'+DateTime.now().millisecond.toString()+':'+DateTime.now().microsecond.toString());
  List<String> stringed = List<String>.empty(growable: true);
  for(int j= 0; j< separated.length;j++){
    stringed.add(utf8.decode(separated[j]));
  }
  print(DateTime.now().minute.toString()+':'+DateTime.now().second.toString()+':'+DateTime.now().millisecond.toString()+':'+DateTime.now().microsecond.toString());
  print('\n');

Slightly custom implementation, using Uint8List, then utf8.decode:

print(DateTime.now().minute.toString()+':'+DateTime.now().second.toString()+':'+DateTime.now().millisecond.toString()+':'+DateTime.now().microsecond.toString());
  final Uint8List fileBytes = File("/tmp/sample.txt").readAsBytesSync();
  print(DateTime.now().minute.toString()+':'+DateTime.now().second.toString()+':'+DateTime.now().millisecond.toString()+':'+DateTime.now().microsecond.toString());
  final String coded = utf8.decode(fileBytes);
  print(DateTime.now().minute.toString()+':'+DateTime.now().second.toString()+':'+DateTime.now().millisecond.toString()+':'+DateTime.now().microsecond.toString());
  final List<String> codeSplit = coded.split('\n');
  print(DateTime.now().minute.toString()+':'+DateTime.now().second.toString()+':'+DateTime.now().millisecond.toString()+':'+DateTime.now().microsecond.toString());
  print('\n');

Using built-in implementations:

print(DateTime.now().minute.toString()+':'+DateTime.now().second.toString()+':'+DateTime.now().millisecond.toString()+':'+DateTime.now().microsecond.toString());
  final String fileString = File("/tmp/sample.txt").readAsStringSync();
  print(DateTime.now().minute.toString()+':'+DateTime.now().second.toString()+':'+DateTime.now().millisecond.toString()+':'+DateTime.now().microsecond.toString());
  List<String> split = fileString.split('\n');
  print(DateTime.now().minute.toString()+':'+DateTime.now().second.toString()+':'+DateTime.now().millisecond.toString()+':'+DateTime.now().microsecond.toString());
  print('\n');

Results:

19:12:114:464
19:12:125:321
19:12:126:64
19:12:128:199


19:12:128:250
19:12:128:387
19:12:128:441
19:12:128:585


19:12:128:629
19:12:128:941
19:12:129:16


Solution

  • The same question was asked at the OFFICIAL git repository of dart, the primary focus of which is given below:

    Different code behave differently.

    The built-in functionality is written to be efficient at their job. The code here is not particularly efficient because it:

    • Copies bytes into new arrays.
    • Reads separated.last for each byte.
    • Read bytes[i] twice.

    github.com/dart-lang/sdk/issues/49018