-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.js
96 lines (90 loc) · 2.28 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const fastStats = require("./src/index")
const sizes = [10, 100, 1000, 2000, 3000];
const iteration = 100;
/**
* Basic computation of stats
* @param {Array} array
* @returns
*/
function computeStats(array) {
let min = Number.MAX_VALUE;
let max = Number.MIN_VALUE;
let variance = 0;
let sum = 0;
let n = array.length;
array.forEach(element => {
if (element < min) {
min = element;
}
if (element > max) {
max = element;
}
sum += element;
});
let avg = sum / n;
array.forEach(element => {
if (element < min) {
min = element;
}
if (element > max) {
max = element;
}
variance += (element - avg) * (element - avg);
});
variance = variance / n;
return {
n: n,
min: min,
max: max,
sum: sum,
mean: avg,
variance: variance,
standard_deviation: Math.sqrt(variance)
};
}
class rollingArray {
constructor(size) {
this.array = [];
this.maxSize = size;
this.pos = 0;
}
push(num) {
if (this.array.length < this.maxSize) {
this.array.push(num);
} else {
if (this.pos >= this.maxSize) {
this.pos -= this.maxSize;
}
this.array[this.pos] = num;
}
this.pos++;
}
}
function benchmark(size) {
let rawData = [];
for (let i = 0; i < 2 * size; i++) {
rawData.push(Math.random());
}
let results = {};
console.time(`simple rollingArray implem, size = ${size}`);
for (let i = 0; i < iteration; i++) {
let myArray = new rollingArray(size);
rawData.forEach((elem) => {
myArray.push(elem);
results.simple = computeStats(myArray.array);
})
}
console.timeEnd(`simple rollingArray implem, size = ${size}`);
console.time(`fastStats implem, size = ${size}`);
for (let i = 0; i < iteration; i++) {
let myArray = new fastStats(size);
rawData.forEach((elem) => {
results.fastStats = myArray.append(elem).getStats();
})
}
console.timeEnd(`fastStats implem, size = ${size}`);
console.log(results);
}
sizes.forEach((size) => {
benchmark(size);
})