Last active
December 13, 2015 22:18
-
-
Save sethladd/4983102 to your computer and use it in GitHub Desktop.
Benchmark map implementations in Dart.
This file contains hidden or 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 'package:benchmark_harness/benchmark_harness.dart'; | |
| import 'dart:collection'; | |
| // Create a new benchmark by extending BenchmarkBase | |
| class MapInsertBenchmark extends BenchmarkBase { | |
| Function makeMap; | |
| MapInsertBenchmark(this.makeMap, String name) : super(name); | |
| // The benchmark code. | |
| void run() { | |
| Map map = makeMap(); | |
| for (int i = 0; i < 10000; i++) { | |
| map[i] = i; | |
| } | |
| } | |
| } | |
| class MapRetrieveBenchmark extends BenchmarkBase { | |
| Function makeMap; | |
| Map map; | |
| int toGet; | |
| MapRetrieveBenchmark(this.makeMap, this.toGet, String name) : super(name); | |
| // The benchmark code. | |
| void run() { | |
| var foo = map[toGet]; | |
| } | |
| void setup() { | |
| map = makeMap(); | |
| for (int i = 0; i < 10000; i++) { | |
| map[i] = i; | |
| } | |
| } | |
| } | |
| main() { | |
| // Run TemplateBenchmark | |
| new MapInsertBenchmark(() => new HashMap(), "HashMap").report(); | |
| new MapInsertBenchmark(() => new SplayTreeMap(), "SplayTreeMap").report(); | |
| new MapInsertBenchmark(() => new LinkedHashMap(), "LinkedHashMap").report(); | |
| new MapRetrieveBenchmark(() => new HashMap(), 1, "HashMap").report(); | |
| new MapRetrieveBenchmark(() => new SplayTreeMap(), 1, "SplayTreeMap").report(); | |
| new MapRetrieveBenchmark(() => new LinkedHashMap(), 1, "LinkedHashMap").report(); | |
| new MapRetrieveBenchmark(() => new HashMap(), 9999, "HashMap").report(); | |
| new MapRetrieveBenchmark(() => new SplayTreeMap(), 9999, "SplayTreeMap").report(); | |
| new MapRetrieveBenchmark(() => new LinkedHashMap(), 9999, "LinkedHashMap").report(); | |
| } |
This file contains hidden or 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
| HashMap(RunTime): 17443.478260869564 us. | |
| SplayTreeMap(RunTime): 31250.0 us. | |
| LinkedHashMap(RunTime): 817000.0 us. | |
| HashMap(RunTime): 0.7287397466317649 us. | |
| SplayTreeMap(RunTime): 2.640243113585899 us. | |
| LinkedHashMap(RunTime): 1.4227564198326554 us. | |
| HashMap(RunTime): 0.7923399740667126 us. | |
| SplayTreeMap(RunTime): 2.406854722248965 us. | |
| LinkedHashMap(RunTime): 1.4545084306944913 us. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment