0

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

gitman-2021
  • 111
  • 8
  • 2
    1. You should describe in the beginning *what* you're trying to do. (Reading a text file and separating it into lines.) 2. Please just use [`Stopwatch`](https://api.dart.dev/stable/dart-core/Stopwatch-class.html) to measure elapsed time. – jamesdlin May 15 '22 at 15:42
  • 2
    3. Version 1 is does strictly more work than version 2 (it calls `utf8.decode` multiple times and performs many more allocations). Versions 2 and 3 should be about the same; `File.readAsStringSync` should be (mostly) equivalent to `File.readAsBytesSync` + `utf8.decode`. (I'd expect `File.readAsStringSync` to be *slightly* slower since I think it does extra work to do platform-dependent newline conversion.) 4. You really should just use [`File.readAsLinesSync`](https://api.dart.dev/stable/dart-io/File/readAsLinesSync.html) anyway. – jamesdlin May 15 '22 at 15:43
  • Hello, very thank you for the suggestions, i will remember first two points, and i understand the third, – gitman-2021 May 16 '22 at 12:44
  • @jamesdlin Please have a look at [this](https://github.com/flutter/flutter/issues/103843) flutter issue, it is working in the release build, but NOT in debug – gitman-2021 May 16 '22 at 12:53
  • What does that issue have to do with this question? – jamesdlin May 16 '22 at 14:43
  • @jamesdlin Actually it is not, but the flutter members are just closing the issue, its working as intended in the release build, but not in the debug one, so there must be something wrong in the debug building – gitman-2021 May 17 '22 at 15:21

1 Answers1

0

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

gitman-2021
  • 111
  • 8
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/31785749) – pringi May 17 '22 at 08:26
  • done, thanking you for suggesting, im a new member, so please forgive – gitman-2021 May 17 '22 at 15:19