Created
November 21, 2019 11:29
-
-
Save dsrenesanse/336a64d0c0a870e8ffb9d2cac48a3289 to your computer and use it in GitHub Desktop.
performance test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
import 'package:built_collection/built_collection.dart'; | |
import 'package:flutter_test/flutter_test.dart'; | |
const iterations = 100000; | |
void main() { | |
test("test", () async { | |
final numbers = List.generate(iterations, (index) => index); | |
///MAP | |
final map = <int, String>{}; | |
numbers.forEach((number) { | |
map[number] = number.toString(); | |
}); | |
final builtMap = MapBuilder<int, String>(); | |
builtMap..addIterable(numbers, key: (key) => key, value: (key) => key.toString()); | |
testBuildMap(builtMap.build()); | |
testMap(map); | |
///List | |
final builtList = ListBuilder<int>(numbers); | |
testBuildList(builtList.build()); | |
testList(numbers); | |
}); | |
} | |
void testBuildMap(BuiltMap<int, String> map) { | |
final timeStart = DateTime.now().millisecondsSinceEpoch; | |
map.rebuild((b) { | |
for (int i = 0; i < iterations; i++) { | |
final randomNumber = Random().nextInt(iterations); | |
b.updateValue(randomNumber, (s) => randomNumber.toString()); | |
} | |
return b; | |
}); | |
print('built map spend: ${(DateTime.now().millisecondsSinceEpoch - timeStart)}'); | |
print('built map spend: ${(DateTime.now().millisecondsSinceEpoch - timeStart) / iterations}'); | |
} | |
void testMap(Map<int, String> map) { | |
final timeStart = DateTime.now().millisecondsSinceEpoch; | |
for (int i = 0; i < iterations; i++) { | |
final randomNumber = Random().nextInt(iterations); | |
map[randomNumber] = randomNumber.toString(); | |
} | |
print('map spend: ${(DateTime.now().millisecondsSinceEpoch - timeStart)}'); | |
print('map spend: ${(DateTime.now().millisecondsSinceEpoch - timeStart) / iterations}'); | |
} | |
void testBuildList(BuiltList<int> list) { | |
final timeStart = DateTime.now().millisecondsSinceEpoch; | |
list.rebuild((b) { | |
for (int i = 0; i < iterations; i++) { | |
final randomNumber = Random().nextInt(iterations); | |
b.update((b) => b[randomNumber] = randomNumber); | |
} | |
return b; | |
}); | |
print('built list spend: ${(DateTime.now().millisecondsSinceEpoch - timeStart)}'); | |
print('built list spend: ${(DateTime.now().millisecondsSinceEpoch - timeStart) / iterations}'); | |
} | |
void testList(List<int> list) { | |
final timeStart = DateTime.now().millisecondsSinceEpoch; | |
for (int i = 0; i < iterations; i++) { | |
final randomNumber = Random().nextInt(iterations); | |
list[randomNumber] = randomNumber; | |
} | |
print('list spend: ${(DateTime.now().millisecondsSinceEpoch - timeStart)}'); | |
print('list spend: ${(DateTime.now().millisecondsSinceEpoch - timeStart) / iterations}'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment