Skip to content

Commit bd4b574

Browse files
authored
Partial fix for benchmarks (#337)
* Fix vector2 benchmark * Remove results * Fix analyzer infos
1 parent 433fb6c commit bd4b574

File tree

1 file changed

+46
-62
lines changed

1 file changed

+46
-62
lines changed

benchmark/vector2_bench.dart

Lines changed: 46 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,144 +8,127 @@ import 'dart:typed_data';
88
import 'package:benchmark_harness/benchmark_harness.dart';
99
import 'package:vector_math/vector_math.dart';
1010

11+
Never _unreachableAssertionError() => throw AssertionError('Unreachable');
12+
1113
class ConstructorBenchmark extends BenchmarkBase {
1214
ConstructorBenchmark() : super('Vector2()');
1315

14-
static void main() {
15-
ConstructorBenchmark().report();
16-
}
17-
1816
@override
1917
void run() {
18+
Vector2? v;
2019
for (var i = 0; i < 100000; i++) {
21-
Vector2(100, 100);
20+
v = Vector2(100, 100);
2221
}
22+
if (v == null) _unreachableAssertionError();
2323
}
2424
}
2525

2626
class ConstructorZeroBenchmark extends BenchmarkBase {
2727
ConstructorZeroBenchmark() : super('Vector2.zero()');
2828

29-
static void main() {
30-
ConstructorZeroBenchmark().report();
31-
}
32-
3329
@override
3430
void run() {
31+
Vector2? v;
3532
for (var i = 0; i < 100000; i++) {
36-
Vector2.zero();
33+
v = Vector2.zero();
3734
}
35+
if (v == null) _unreachableAssertionError();
3836
}
3937
}
4038

4139
class ConstructorArrayBenchmark extends BenchmarkBase {
4240
ConstructorArrayBenchmark() : super('Vector2.array()');
4341

44-
static void main() {
45-
ConstructorArrayBenchmark().report();
46-
}
47-
4842
@override
4943
void run() {
44+
Vector2? v;
5045
for (var i = 0.0; i < 100000; i++) {
51-
Vector2.array([i, i]);
46+
v = Vector2.array([i, i]);
5247
}
48+
if (v == null) _unreachableAssertionError();
5349
}
5450
}
5551

5652
class ConstructorAllBenchmark extends BenchmarkBase {
5753
ConstructorAllBenchmark() : super('Vector2.all()');
5854

59-
static void main() {
60-
ConstructorAllBenchmark().report();
61-
}
62-
6355
@override
6456
void run() {
57+
Vector2? v;
6558
for (var i = 0.0; i < 100000; i++) {
66-
Vector2.all(i);
59+
v = Vector2.all(i);
6760
}
61+
if (v == null) _unreachableAssertionError();
6862
}
6963
}
7064

7165
class ConstructorCopyBenchmark extends BenchmarkBase {
7266
ConstructorCopyBenchmark() : super('Vector2.copy()');
7367

74-
static void main() {
75-
ConstructorCopyBenchmark().report();
76-
}
77-
7868
@override
7969
void run() {
70+
Vector2? v;
8071
final copyFrom = Vector2(1, 1);
8172
for (var i = 0.0; i < 100000; i++) {
82-
Vector2.copy(copyFrom);
73+
v = Vector2.copy(copyFrom);
8374
}
75+
if (v == null) _unreachableAssertionError();
8476
}
8577
}
8678

8779
class ConstructorFromFloat32ListBenchmark extends BenchmarkBase {
8880
ConstructorFromFloat32ListBenchmark() : super('Vector2.fromFloat32List()');
8981

90-
static void main() {
91-
ConstructorFromFloat32ListBenchmark().report();
92-
}
93-
9482
@override
9583
void run() {
84+
Vector2? v;
9685
final list = Float32List.fromList([0.0, 0.0]);
9786
for (var i = 0.0; i < 100000; i++) {
98-
Vector2.fromFloat32List(list);
87+
v = Vector2.fromFloat32List(list);
9988
}
89+
if (v == null) _unreachableAssertionError();
10090
}
10191
}
10292

10393
class ConstructorFromBufferBenchmark extends BenchmarkBase {
10494
ConstructorFromBufferBenchmark() : super('Vector2.fromBuffer()');
10595

106-
static void main() {
107-
ConstructorFromBufferBenchmark().report();
108-
}
109-
11096
@override
11197
void run() {
98+
Vector2? v;
11299
final buffer = Uint32List(2).buffer;
113100
for (var i = 0.0; i < 100000; i++) {
114-
Vector2.fromBuffer(buffer, 0);
101+
v = Vector2.fromBuffer(buffer, 0);
115102
}
103+
if (v == null) _unreachableAssertionError();
116104
}
117105
}
118106

119107
class ConstructorRandomBenchmark extends BenchmarkBase {
120108
ConstructorRandomBenchmark() : super('Vector2.random()');
121109

122-
static void main() {
123-
ConstructorRandomBenchmark().report();
124-
}
125-
126110
@override
127111
void run() {
112+
Vector2? v;
128113
final random = math.Random();
129114
for (var i = 0.0; i < 100000; i++) {
130-
Vector2.random(random);
115+
v = Vector2.random(random);
131116
}
117+
if (v == null) _unreachableAssertionError();
132118
}
133119
}
134120

135121
class SetFromBenchmark extends BenchmarkBase {
136122
SetFromBenchmark() : super('Vector2.setFrom()');
137-
final Vector2 v1 = Vector2(100, 100);
138-
final Vector2 v2 = Vector2.zero();
139-
140-
static void main() {
141-
SetFromBenchmark().report();
142-
}
123+
final Vector2 source = Vector2(100, 100);
143124

144125
@override
145126
void run() {
127+
var v = Vector2.zero();
146128
for (var i = 0; i < 100000; i++) {
147-
v2.setFrom(v1);
129+
v = v..setFrom(source);
148130
}
131+
if (v.x != 100 || v.y != 100) _unreachableAssertionError();
149132
}
150133
}
151134

@@ -154,27 +137,28 @@ class DotProductBenchmark extends BenchmarkBase {
154137
final Vector2 v1 = Vector2(100, 100);
155138
final Vector2 v2 = Vector2(100, 200);
156139

157-
static void main() {
158-
DotProductBenchmark().report();
159-
}
160-
161140
@override
162141
void run() {
142+
var r = .0;
163143
for (var i = 0; i < 100000; i++) {
164-
v1.dot(v2);
144+
r += v1.dot(v2);
165145
}
146+
if (r != 30000 * 100000) _unreachableAssertionError();
166147
}
167148
}
168149

169150
void main() {
170-
ConstructorBenchmark.main();
171-
ConstructorZeroBenchmark.main();
172-
ConstructorArrayBenchmark.main();
173-
ConstructorAllBenchmark.main();
174-
ConstructorCopyBenchmark.main();
175-
ConstructorFromFloat32ListBenchmark.main();
176-
ConstructorFromBufferBenchmark.main();
177-
ConstructorRandomBenchmark.main();
178-
SetFromBenchmark.main();
179-
DotProductBenchmark.main();
151+
void report(BenchmarkBase Function() create) => create().report();
152+
[
153+
ConstructorBenchmark.new,
154+
ConstructorZeroBenchmark.new,
155+
ConstructorArrayBenchmark.new,
156+
ConstructorAllBenchmark.new,
157+
ConstructorCopyBenchmark.new,
158+
ConstructorFromFloat32ListBenchmark.new,
159+
ConstructorFromBufferBenchmark.new,
160+
ConstructorRandomBenchmark.new,
161+
SetFromBenchmark.new,
162+
DotProductBenchmark.new,
163+
].forEach(report);
180164
}

0 commit comments

Comments
 (0)