为什么这个自定义实现比内置的慢?

Why this custom implementation slower than the built-in?

请问,为什么这个用户自制的实现比内置的慢:

样本:

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

自定义实现,使用Uint8List,for循环拆分字节序列列表:

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');

稍微自定义实现,使用Uint8List,然后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');

使用内置实现:

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');

结果:

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

在 dart 的官方 git 存储库中提出了同样的问题,其主要焦点如下:

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