Skip to content

Instantly share code, notes, and snippets.

@fabads
Last active May 1, 2020 07:47
Show Gist options
  • Save fabads/2ff65436310b4a046090422ee098f39d to your computer and use it in GitHub Desktop.
Save fabads/2ff65436310b4a046090422ee098f39d to your computer and use it in GitHub Desktop.
This quick and dirty code evaluates the code performance to convert strings with leading zeros to strings without leading zeros. 2 methods are used: double conversion String to int then int to String and regular expressions
void main() {
// Returns empty string
String s00 = '00';
String s0 = s00.replaceAll(new RegExp(r'^[0]*'), '');
print("convesion of 00 with RegExp returns empty string: '$s0'");
String s01 = '01';
String s02 = '02';
String s03 = '03';
String s04 = '04';
String s05 = '05';
String s06 = '06';
String s07 = '07';
String s08 = '08';
String s09 = '09';
Stopwatch stopwatch = Stopwatch()..start();
RegExp regexp = RegExp(r'^[0]*');
for (int i = 0; i < 999999; i++) {
String s1 = s01.replaceAll(regexp, '');
String s2 = s02.replaceAll(regexp, '');
String s3 = s03.replaceAll(regexp, '');
String s4 = s04.replaceAll(regexp, '');
String s5 = s05.replaceAll(regexp, '');
String s6 = s06.replaceAll(regexp, '');
String s7 = s07.replaceAll(regexp, '');
String s8 = s08.replaceAll(regexp, '');
String s9 = s09.replaceAll(regexp, '');
}
print('RegExp executed in ${stopwatch.elapsed}');
stopwatch = Stopwatch()..start();
for (int i = 0; i < 999999; i++) {
String s1 = int.parse(s01).toString();
String s2 = int.parse(s02).toString();
String s3 = int.parse(s03).toString();
String s4 = int.parse(s04).toString();
String s5 = int.parse(s05).toString();
String s6 = int.parse(s06).toString();
String s7 = int.parse(s07).toString();
String s8 = int.parse(s08).toString();
String s9 = int.parse(s09).toString();
}
print('Double conversion executed in ${stopwatch.elapsed}');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment