-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.ts
165 lines (144 loc) · 4.19 KB
/
benchmark.ts
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import NSet from './lib/NSet';
const N = 5;
const NS_PER_SEC = 1e9;
function randInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const setItems = [];
for (let i = 0; i < 1e6; i++) {
const it = [];
const len = randInt(1, 4);
for (let j = 0; j < len; j++) {
it.push(randInt(0, 999));
}
setItems.push(it);
}
console.log(`Data distribution:\n------------`);
console.log(`= 1-depth - ${setItems.filter(it => it.length === 1).length}`);
console.log(`= 2-depth - ${setItems.filter(it => it.length === 2).length}`);
console.log(`= 3-depth - ${setItems.filter(it => it.length === 3).length}`);
console.log(`= 4-depth - ${setItems.filter(it => it.length === 4).length}\n\n`);
const tests = [];
const setItems1 = setItems.filter(it => it.length === 1);
tests.push(() => {
const map = new Map();
for (let i = 0; i < setItems1.length; i++) {
map.set(setItems1[i][0], true);
}
for (let i = 0; i < setItems1.length; i++) {
if (!map.has(setItems1[i][0])) {
return false;
}
}
for (let i = setItems1.length - 1; i >= 0; i--) {
map.delete(setItems1[i][0]);
}
return {name: 'Map', value: map.size};
});
tests.push(() => {
const set = new Set();
for (let i = 0; i < setItems1.length; i++) {
set.add(setItems1[i][0]);
}
for (let i = 0; i < setItems1.length; i++) {
if (!set.has(setItems1[i][0])) {
return false;
}
}
for (let i = setItems1.length - 1; i >= 0; i--) {
set.delete(setItems1[i][0]);
}
return {name: 'Set', value: set.size};
});
tests.push(() => {
const set = new NSet();
for (let i = 0; i < setItems1.length; i++) {
set.add(setItems1[i]);
}
for (let i = 0; i < setItems1.length; i++) {
if (!set.has(setItems1[i])) {
return false;
}
}
for (let i = setItems1.length - 1; i >= 0; i--) {
set.delete(setItems1[i]);
}
return {name: 'NSet 1-depth', value: set.size};
});
const setItems2 = setItems.filter(it => it.length === 2);
tests.push(() => {
const set = new NSet();
for (let i = 0; i < setItems2.length; i++) {
set.add(...setItems2[i]);
}
for (let i = 0; i < setItems2.length; i++) {
if (!set.has(...setItems2[i])) {
return false;
}
}
for (let i = setItems2.length - 1; i >= 0; i--) {
set.delete(...setItems2[i]);
}
return {name: 'NSet 2-depth', value: set.size};
});
const setItems3 = setItems.filter(it => it.length === 3);
tests.push(() => {
const set = new NSet();
for (let i = 0; i < setItems3.length; i++) {
set.add(...setItems3[i]);
}
for (let i = 0; i < setItems3.length; i++) {
if (!set.has(...setItems3[i])) {
return false;
}
}
for (let i = setItems3.length - 1; i >= 0; i--) {
set.delete(...setItems3[i]);
}
return {name: 'NSet 3-depth', value: set.size};
});
tests.push(() => {
const set = new NSet();
for (let i = 0; i < setItems.length; i++) {
set.add(...setItems[i]);
}
for (let i = 0; i < setItems.length; i++) {
if (!set.has(...setItems[i])) {
return false;
}
}
for (let i = setItems.length - 1; i >= 0; i--) {
set.delete(...setItems[i]);
}
return {name: 'NSet all-depth', value: set.size};
});
const results = [];
for (let n = 0; n < N; n++) {
tests.forEach((test, key) => {
if (!results[key]) {
results[key] = {
results: [],
timings: []
};
}
const startTime = process.hrtime();
const res = tests[key](); // run test
const duration = process.hrtime(startTime);
results[key].results.push(res);
results[key].timings.push((duration[0] * NS_PER_SEC + duration[1]) / 1000000);
});
}
function formatTiming (ms) {
return `${ms.toFixed(3)} ms`;
}
console.log(`After ${N} runs:\n------------`);
results.forEach(result => {
result.timings.sort();
const sum = result.timings.reduce((sum, x) => sum + x);
const avg = sum / N;
const middle = Math.floor(N / 2);
const median = N % 2 === 0 ? (result.timings[middle] + result.timings[middle - 1]) / 2 : result.timings[middle];
const min = Math.min(...result.timings);
const max = Math.max(...result.timings);
console.log(`${result.results[0].name.padEnd(20)} - Min: ${formatTiming(min)}, Max: ${formatTiming(max)}, Mean: ${formatTiming(avg)}, Median: ${formatTiming(median)}`);
});