-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
MapLookup.dart
131 lines (100 loc) · 3.2 KB
/
MapLookup.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Benchmark for https://github.com/dart-lang/sdk/issues/45908.
//
// Measures the average time needed for a lookup in Maps.
import 'dart:math';
import 'maps.dart';
abstract class MapLookupBenchmark {
final String name;
const MapLookupBenchmark(this.name);
Map<String, String> get myMap;
// Returns the number of nanoseconds per call.
double measureFor(Duration duration) {
final map = myMap;
// Prevent `sw.elapsedMicroseconds` from dominating with maps with a
// small number of elements.
final int batching = max(1000 ~/ map.length, 1);
int numberOfLookups = 0;
int totalMicroseconds = 0;
final sw = Stopwatch()..start();
final durationInMicroseconds = duration.inMicroseconds;
do {
for (int i = 0; i < batching; i++) {
String? k = '0';
while (k != null) {
k = map[k];
}
numberOfLookups += map.length;
}
totalMicroseconds = sw.elapsedMicroseconds;
} while (totalMicroseconds < durationInMicroseconds);
final int totalNanoseconds = sw.elapsed.inMicroseconds * 1000;
return totalNanoseconds / numberOfLookups;
}
// Runs warmup phase, runs benchmark and reports result.
void report() {
// Warmup for 100 ms.
measureFor(const Duration(milliseconds: 100));
// Run benchmark for 2 seconds.
final double nsPerCall = measureFor(const Duration(seconds: 2));
// Report result.
print('$name(RunTimeRaw): $nsPerCall ns.');
}
}
class Constant1 extends MapLookupBenchmark {
const Constant1() : super('MapLookup.Constant1');
@override
Map<String, String> get myMap => const1;
}
class Final1 extends MapLookupBenchmark {
const Final1() : super('MapLookup.Final1');
@override
Map<String, String> get myMap => final1;
}
class Constant5 extends MapLookupBenchmark {
const Constant5() : super('MapLookup.Constant5');
@override
Map<String, String> get myMap => const5;
}
class Final5 extends MapLookupBenchmark {
const Final5() : super('MapLookup.Final5');
@override
Map<String, String> get myMap => final5;
}
class Constant10 extends MapLookupBenchmark {
const Constant10() : super('MapLookup.Constant10');
@override
Map<String, String> get myMap => const10;
}
class Final10 extends MapLookupBenchmark {
const Final10() : super('MapLookup.Final10');
@override
Map<String, String> get myMap => final10;
}
class Constant100 extends MapLookupBenchmark {
const Constant100() : super('MapLookup.Constant100');
@override
Map<String, String> get myMap => const100;
}
class Final100 extends MapLookupBenchmark {
const Final100() : super('MapLookup.Final100');
@override
Map<String, String> get myMap => final100;
}
void main() {
final benchmarks = [
() => const Constant1(),
() => const Constant5(),
() => const Constant10(),
() => const Constant100(),
() => const Final1(),
() => const Final5(),
() => const Final10(),
() => const Final100(),
];
for (final benchmark in benchmarks) {
benchmark().report();
}
}