-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.js
executable file
·72 lines (54 loc) · 1.67 KB
/
bench.js
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
const getRandomLocations = (n) => {
let m_w = 123456789;
let m_z = 987654321;
let mask = 0xffffffff;
// Taken from https://stackoverflow.com/a/19301306
const random = () => {
m_z = (36969 * (m_z & 65535) + (m_z >> 16)) & mask;
m_w = (18000 * (m_w & 65535) + (m_w >> 16)) & mask;
let result = ((m_z << 16) + m_w) & mask;
result /= 4294967296;
return result + 0.5;
}
const locs = [];
for (let i = 0; i < n; i++) {
locs.push({
x: random() * 100,
y: random() * 100
})
}
return locs;
}
const distBenchmark = (timeLimit) => {
const locs = getRandomLocations(10);
const start = new Date().getTime();
let ops = 0;
let curTime = new Date().getTime();
while (curTime < start + timeLimit) {
const dists = [];
locs.forEach(a => locs.forEach(b => {
const d = Math.sqrt(Math.pow(b.x - a.x, 2) + Math.pow(b.y - a.y, 2));
dists.push(d);
}))
ops++;
curTime = new Date().getTime();
}
return ops;
}
const fibonacci = i => {
if (i <= 1) return i;
return fibonacci(i - 1) + fibonacci(i - 2);
}
const fibBenchmark = i => {
const start = new Date().getTime();
fibonacci(i);
const end = new Date().getTime();
return end - start;
}
const duration = fibBenchmark(41) // Returns time required (ms)
// to calculate the 41. fibonacci number recursively.
const ops = distBenchmark(1000) // Returns the amount of operations
// (distance matrix calculations) in 1000ms
console.log(`fibonacci(41) duration: ${duration} ms`);
console.log(`distance matrix calculations in 1 sec: ${ops}`);
///