-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark.js
90 lines (64 loc) · 1.53 KB
/
benchmark.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
/**
* Test flat over nested implementations
*
* Results.
* Nested implementation is just a bit slower to create than plain typed array, but way faster to access/write channel data. We can cache subarrays to avoid that.
*
*/
const t = require('tape')
function Nested(ch, len) {
this.data = []
for (let i = 0; i < ch; i++) {
this.data[i] = new Float32Array(len)
}
}
Nested.prototype.getChannelData = function (c) {
return this.data[c]
}
function Flat(ch, len) {
this.length = len
this.data = new Float32Array(ch * len)
this.channels = []
for (let i = 0; i < ch; i++) {
this.channels[i] = this.data.subarray(i * len, (i+1) * len)
}
}
Flat.prototype.getChannelData = function (c) {
return this.channels[c]
}
//test
let N = 1e4
t('flat create', t => {
console.time('flat create')
for (let i = N; i--;) {
let a = new Flat(4, i)
}
console.timeEnd('flat create')
t.end()
})
t('nested create', t => {
console.time('nested create')
for (let i = N; i--;) {
let a = new Nested(4, i)
}
console.timeEnd('nested create')
t.end()
})
t.test('nested getChannelData', t => {
console.time('nested getChannelData')
let a = new Nested(4, 1e5)
for (let i = N; i--;) {
let data = a.getChannelData(Math.floor(Math.random() * 4))
}
console.timeEnd('nested getChannelData')
t.end()
})
t.test('flat getChannelData', t => {
console.time('flat getChannelData')
let a = new Flat(4, 1e5)
for (let i = N; i--;) {
let data = a.getChannelData(Math.floor(Math.random() * 4))
}
console.timeEnd('flat getChannelData')
t.end()
})