Skip to content

Commit dd38b37

Browse files
committed
added benchmarks against differ
1 parent f9648b1 commit dd38b37

File tree

3 files changed

+166
-1
lines changed

3 files changed

+166
-1
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,15 @@ class Headbutt {
114114
*/
115115
public function test(a: Shape, b: Shape): Bool;
116116
}
117-
```
117+
```
118+
119+
## Benchmarks
120+
121+
| Test | Intersect | Headbutt (μs/iter) | Differ (μs/iter) |
122+
|:-----|:---------:|---------:|-------:|
123+
| line/line || 17.6 ± 0.4 | 1.1 ± 0 |
124+
| line/line || 11.2 ± 0.5 | 0.9 ± 0 |
125+
| circ/circ || 21.7 ± 0.2 | 3 ± 0 |
126+
| circ/circ || 4.8 ± 0.1 | 1.6 ± 0 |
127+
| pent/pent || 41.3 ± 0.4 | 31.5 ± 0.3 |
128+
| pent/pent || 11.6 ± 0.1 | 7.4 ± 0 |

bench.interp.hxml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-lib glm
2+
-lib differ
3+
-cp bench
4+
-cp src
5+
-main Main
6+
--interp

bench/Main.hx

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package;
2+
3+
import headbutt.twod.Headbutt;
4+
import haxe.ds.Vector;
5+
import haxe.Timer;
6+
import glm.Vec2;
7+
import differ.Collision;
8+
9+
class Benchmark {
10+
var results: Vector<Float>;
11+
public var mean(get, never): Float;
12+
public var std(get, never): Float;
13+
14+
public function new(count: Int = 100) {
15+
results = new Vector(count);
16+
}
17+
18+
static function bench(f: Void->Void, times: Int): Float {
19+
var start: Float = Timer.stamp();
20+
for(i in 0...times) f();
21+
var end: Float = Timer.stamp();
22+
23+
return (end - start) / times;
24+
}
25+
26+
public function run(f: Void->Void, times: Int = 1000) {
27+
for(i in 0...results.length) {
28+
results[i] = bench(f, times);
29+
}
30+
}
31+
32+
function get_mean(): Float {
33+
var sum: Float = 0;
34+
for(result in results) {
35+
sum += result;
36+
}
37+
return sum / results.length;
38+
}
39+
40+
function get_std(): Float {
41+
var mean: Float = this.mean;
42+
var sum: Float = 0;
43+
for(result in results) {
44+
sum += (result - mean) * (result - mean);
45+
}
46+
return Math.sqrt(sum / results.length);
47+
}
48+
49+
static function disp_micro(x: Float): String {
50+
x = Math.fround(x * 10000000) / 10.0;
51+
return Std.string(x);
52+
}
53+
54+
public function toString(): String {
55+
return '${disp_micro(mean)} ± ${disp_micro(std)}';
56+
}
57+
}
58+
59+
class Main {
60+
public static function main(): Void {
61+
var hb: Headbutt = new Headbutt();
62+
Sys.println('| Test | Intersect | Headbutt (μs/iter) | Differ (μs/iter) |');
63+
Sys.println('|:-----|:---------:|---------:|-------:|');
64+
65+
var lineA = new headbutt.twod.shapes.Line(new Vec2(-1, -1), new Vec2(1, 1));
66+
var lineB = new headbutt.twod.shapes.Line(new Vec2(-1, 1), new Vec2(1, -1));
67+
var lineC = new headbutt.twod.shapes.Line(new Vec2(1, -1), new Vec2(2, 1));
68+
var rayA = new differ.shapes.Ray(new differ.math.Vector(-1, -1), new differ.math.Vector(1, 1));
69+
var rayB = new differ.shapes.Ray(new differ.math.Vector(-1, 1), new differ.math.Vector(1, -1));
70+
var rayC = new differ.shapes.Ray(new differ.math.Vector(1, -1), new differ.math.Vector(2, 1));
71+
72+
var line_line_int_hb = new Benchmark();
73+
line_line_int_hb.run(function() {
74+
hb.test(lineA, lineB);
75+
});
76+
var line_line_int_diff = new Benchmark();
77+
line_line_int_diff.run(function() {
78+
Collision.rayWithRay(rayA, rayB);
79+
});
80+
Sys.println('| line/line | ✔ | ${line_line_int_hb.toString()} | ${line_line_int_diff.toString()} |');
81+
82+
var line_line_noint_hb = new Benchmark();
83+
line_line_noint_hb.run(function() {
84+
hb.test(lineA, lineC);
85+
});
86+
var line_line_noint_diff = new Benchmark();
87+
line_line_noint_diff.run(function() {
88+
Collision.rayWithRay(rayA, rayC);
89+
});
90+
Sys.println('| line/line | ✗ | ${line_line_noint_hb.toString()} | ${line_line_noint_diff.toString()} |');
91+
92+
var circleA = new headbutt.twod.shapes.Circle(new Vec2(0, 0), 0.5);
93+
var circleB = new headbutt.twod.shapes.Circle(new Vec2(0.5, 0), 0.5);
94+
var circleC = new headbutt.twod.shapes.Circle(new Vec2(3, 0), 0.5);
95+
var dCircleA = new differ.shapes.Circle(0, 0, 0.5);
96+
var dCircleB = new differ.shapes.Circle(0.5, 0, 0.5);
97+
var dCircleC = new differ.shapes.Circle(3, 0, 0.5);
98+
99+
var circ_circ_int_hb = new Benchmark();
100+
circ_circ_int_hb.run(function() {
101+
hb.test(circleA, circleB);
102+
});
103+
var circ_circ_int_diff = new Benchmark();
104+
circ_circ_int_diff.run(function() {
105+
Collision.shapeWithShape(dCircleA, dCircleB);
106+
});
107+
Sys.println('| circ/circ | ✔ | ${circ_circ_int_hb.toString()} | ${circ_circ_int_diff.toString()} |');
108+
109+
var circ_circ_noint_hb = new Benchmark();
110+
circ_circ_noint_hb.run(function() {
111+
hb.test(circleA, circleC);
112+
});
113+
var circ_circ_noint_diff = new Benchmark();
114+
circ_circ_noint_diff.run(function() {
115+
Collision.shapeWithShape(dCircleA, dCircleC);
116+
});
117+
Sys.println('| circ/circ | ✗ | ${circ_circ_noint_hb.toString()} | ${circ_circ_noint_diff.toString()} |');
118+
119+
var pentA = new headbutt.twod.shapes.Polygon([new Vec2(0, 1), new Vec2(1, 0.5), new Vec2(1, -1), new Vec2(-1, -1), new Vec2(-1, 0.5)]);
120+
var pentB = new headbutt.twod.shapes.Polygon([new Vec2(0, 1), new Vec2(1, 0.5), new Vec2(1, -1), new Vec2(-1, -1), new Vec2(-1, 0.5)]);
121+
pentB.set_trs(new Vec2(0.5, 0), 0, new Vec2(1, 1));
122+
var pentC = new headbutt.twod.shapes.Polygon([new Vec2(0, 1), new Vec2(1, 0.5), new Vec2(1, -1), new Vec2(-1, -1), new Vec2(-1, 0.5)]);
123+
pentC.set_trs(new Vec2(5, 0), 0, new Vec2(1, 1));
124+
var dpentA = new differ.shapes.Polygon(0, 0, [new differ.math.Vector(0, 1), new differ.math.Vector(1, 0.5), new differ.math.Vector(1, -1), new differ.math.Vector(-1, -1), new differ.math.Vector(-1, 0.5)]);
125+
var dpentB = new differ.shapes.Polygon(0.5, 0, [new differ.math.Vector(0, 1), new differ.math.Vector(1, 0.5), new differ.math.Vector(1, -1), new differ.math.Vector(-1, -1), new differ.math.Vector(-1, 0.5)]);
126+
var dpentC = new differ.shapes.Polygon(5, 0, [new differ.math.Vector(0, 1), new differ.math.Vector(1, 0.5), new differ.math.Vector(1, -1), new differ.math.Vector(-1, -1), new differ.math.Vector(-1, 0.5)]);
127+
128+
var pent_pent_int_hb = new Benchmark();
129+
pent_pent_int_hb.run(function() {
130+
hb.test(pentA, pentB);
131+
});
132+
var pent_pent_int_diff = new Benchmark();
133+
pent_pent_int_diff.run(function() {
134+
Collision.shapeWithShape(dpentA, dpentB);
135+
});
136+
Sys.println('| pent/pent | ✔ | ${pent_pent_int_hb.toString()} | ${pent_pent_int_diff.toString()} |');
137+
138+
var pent_pent_noint_hb = new Benchmark();
139+
pent_pent_noint_hb.run(function() {
140+
hb.test(pentA, pentC);
141+
});
142+
var pent_pent_noint_diff = new Benchmark();
143+
pent_pent_noint_diff.run(function() {
144+
Collision.shapeWithShape(dpentA, dpentC);
145+
});
146+
Sys.println('| pent/pent | ✗ | ${pent_pent_noint_hb.toString()} | ${pent_pent_noint_diff.toString()} |');
147+
}
148+
}

0 commit comments

Comments
 (0)