-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbench.ts
More file actions
95 lines (76 loc) · 2.42 KB
/
bench.ts
File metadata and controls
95 lines (76 loc) · 2.42 KB
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
import Delaunator from 'delaunator';
import Constrainautor from './Constrainautor';
import cdt2d from 'cdt2d';
import os from 'os';
import {findTest, loadTests, TestFile} from './delaunaytests/loader';
const COUNT = 100;
type P2 = [number, number];
type Triangulator = (points: P2[], edges: P2[]) => void
function triangulateCdt2d(points: P2[], edges: P2[]){
cdt2d(points, edges);
}
function triangulateCon(points: P2[], edges: P2[]){
const del = Delaunator.from(points);
const con = new Constrainautor(del);
con.delaunify(true);
return con.constrainAll(edges);
}
export function fmtTime(ns: number | bigint){
return Number((BigInt(ns) / 1000n) | 0n);// + ' μs';
}
export function median(arr: number[] | bigint[]){
const half = (arr.length / 2) | 0;
return arr.length % 2 ? (BigInt(arr[half]) + BigInt(arr[half + 1])) / 2n : arr[half];
}
export function summarizeTimes(times: bigint[]){
times.sort((a, b) => a < b ? -1 : 1);
return {
min: fmtTime(times[0]),
max: fmtTime(times[times.length - 1]),
median: fmtTime(median(times)),
mean: fmtTime(times.reduce((a, b) => a + b, 0n) / BigInt(times.length))
};
}
function benchOne(count: number, json: {points: P2[], edges: P2[], error?: string}, triangulate: Triangulator){
if(json.error){
return;
}
const points = json.points;
const edges = json.edges;
const times: bigint[] = [];
// warmup
triangulate(points, edges);
triangulate(points, edges);
for(let i = 0; i < count; i++){
const start = process.hrtime.bigint();
triangulate(points, edges);
const end = process.hrtime.bigint();
times.push(end - start);
}
return summarizeTimes(times);
}
function benchFiles(files: ReturnType<typeof loadTests>, count: number, triangulate: Triangulator){
const results = [];
for(const json of files){
if(!json.error){
results.push({
name: json.name,
points: json.points.length,
edges: json.edges.length,
...benchOne(count, json, triangulate)
});
}
}
console.table(results);
}
function main(args: string[]){
const testFiles = loadTests(false);
const files = args.length ? args.map(name => findTest(testFiles, name)).filter(t => !!t) as TestFile[] : testFiles;
console.log("Benchmarked on", os.cpus()[0].model, "with",
COUNT, "triangulations per file. Times in µs.");
console.log("cdt2d");
benchFiles(files, COUNT, triangulateCdt2d);
console.log("Constrainautor");
benchFiles(files, COUNT, triangulateCon);
}
main(process.argv.slice(2));