-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
main.dart
133 lines (117 loc) · 3.39 KB
/
main.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
132
133
import 'package:benchmarking/benchmarking.dart';
import 'package:equatable/equatable.dart';
class EmptyEquatable extends Equatable {
@override
List<Object> get props => [];
}
class PrimitiveEquatable extends Equatable {
const PrimitiveEquatable({
required this.integer,
required this.string,
required this.boolean,
});
final int integer;
final String string;
final bool boolean;
@override
List<Object> get props => [integer, string, boolean];
}
class CollectionEquatable extends Equatable {
const CollectionEquatable({
required this.list,
required this.map,
required this.set,
});
final List<int> list;
final Map<String, int> map;
final Set<int> set;
@override
List<Object> get props => [list, map, set];
}
void main() {
_runBenchmark('EmptyEquatable', (_) => EmptyEquatable());
_runBenchmark(
'PrimitiveEquatable',
(index) => PrimitiveEquatable(
integer: index,
string: '$index',
boolean: index.isEven,
),
);
_runBenchmark(
'CollectionEquatable (static, small)',
(index) => CollectionEquatable(
list: List.generate(1, (_) => 42),
map: Map.fromEntries(
// ignore: prefer_const_constructors
List.generate(1, (_) => MapEntry('42', 42)),
),
set: Set.from(List.generate(1, (_) => 42)),
),
);
_runBenchmark(
'CollectionEquatable (static, medium)',
(index) => CollectionEquatable(
list: List.generate(10, (_) => 42),
map: Map.fromEntries(
// ignore: prefer_const_constructors
List.generate(10, (_) => MapEntry('42', 42)),
),
set: Set.from(List.generate(10, (_) => 42)),
),
);
_runBenchmark(
'CollectionEquatable (static, large)',
(index) => CollectionEquatable(
list: List.generate(100, (_) => 42),
map: Map.fromEntries(
// ignore: prefer_const_constructors
List.generate(100, (_) => MapEntry('42', 42)),
),
set: Set.from(List.generate(100, (_) => 42)),
),
);
_runBenchmark(
'CollectionEquatable (dynamic, small)',
(index) => CollectionEquatable(
list: List.generate(1, (i) => index + i),
map: Map.fromEntries(
List.generate(1, (i) => MapEntry('${index + i}', index + i)),
),
set: Set.from(List.generate(1, (i) => index + i)),
),
);
_runBenchmark(
'CollectionEquatable (dynamic, medium)',
(index) => CollectionEquatable(
list: List.generate(10, (i) => index + i),
map: Map.fromEntries(
List.generate(10, (i) => MapEntry('${index + i}', index + i)),
),
set: Set.from(List.generate(10, (i) => index + i)),
),
);
_runBenchmark(
'CollectionEquatable (dynamic, large)',
(index) => CollectionEquatable(
list: List.generate(100, (i) => index + i),
map: Map.fromEntries(
List.generate(100, (i) => MapEntry('${index + i}', index + i)),
),
set: Set.from(List.generate(100, (i) => index + i)),
),
);
}
void _runBenchmark(String name, Object Function(int index) create) {
const poolSize = 100;
final pool = List.generate(poolSize, create);
final poolA = [...pool]..shuffle();
final poolB = [...pool]..shuffle();
bool? result; // so that the loop isn't optimized out
syncBenchmark(name, () {
for (var i = 0; i < poolSize; i++) {
result = poolA[i] == poolB[i];
}
}).report(units: poolSize);
assert(result != null, 'result should be defined.');
}