-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
216 lines (175 loc) · 5.88 KB
/
index.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
'use strict';
const external = {
fftjs: require('fft.js'),
jensnockert: require('fft'),
dspjs: require('dsp.js'),
drom: require('fourier'),
fourierTransform: require('fourier-transform'),
ooura: require('ooura')
};
const benchmark = require('benchmark');
function regexFilter(value) {
if (value !== undefined && value.length !== 0)
return new RegExp(value, 'ig');
else
return /./g;
}
/*eslint-disable no-undef */
const filter0 = regexFilter(process.argv[2]);
const filter1 = regexFilter(process.argv[3]);
/*eslint-enable no-undef */
function addFiltered(suite, name, body) {
if (name.match(filter1) === null)
return;
suite.add(name, body);
}
function createInput(size) {
const res = new Array(size);
for (let i = 0; i < res.length; i++)
res[i] = Math.random() * 2 - 1;
return res;
}
function construct(size) {
const suite = new benchmark.Suite();
addFiltered(suite, 'fft.js', () => {
new external.fftjs(size);
});
addFiltered(suite, 'ooura', () => {
new external.ooura(size);
});
return suite;
}
function addFftjs(suite, size) {
const f = new external.fftjs(size);
const input = createInput(f.size);
const data = f.toComplexArray(input);
const out = f.createComplexArray();
addFiltered(suite, 'fft.js', () => {
f.transform(out, data);
});
}
function addOouraReal4(suite, size) {
const f = new external.ooura(size, {"type":"real", "radix":4});
const input = f.scalarArrayFactory();
addFiltered(suite, 'ooura-radix-4', () => {
f.fftInPlace(input.buffer);
});
}
function addOouraReal8(suite, size) {
const f = new external.ooura(size, {"type":"real", "radix":8});
const input = f.scalarArrayFactory();
addFiltered(suite, 'ooura-radix-8', () => {
f.fftInPlace(input.buffer);
});
}
function addOouraComplex4(suite, size) {
const f = new external.ooura(size, {"type":"complex", "radix":4});
const input = f.scalarArrayFactory();
addFiltered(suite, 'ooura-radix-4', () => {
f.fftInPlace(input.buffer);
});
}
function addOouraComplex8(suite, size) {
const f = new external.ooura(size, {"type":"complex", "radix":8});
const input = f.scalarArrayFactory();
addFiltered(suite, 'ooura-radix-8', () => {
f.fftInPlace(input.buffer);
});
}
function addJensNockert(suite, size) {
const f = new external.jensnockert.complex(size, false);
const input = createInput(size * 2);
const output = new Array(size * 2);
addFiltered(suite, 'jensnockert', () => {
f.simple(output, input, 'complex');
});
}
function addDSPJS(suite, size) {
const f = new external.dspjs.FFT(size, 44100);
// Make benchmark fair
f.calculateSpectrum = function nop() {};
const input = createInput(size);
addFiltered(suite, 'dsp.js', () => {
f.forward(input);
});
}
function addDrom(suite, size) {
const heap = external.drom.custom.alloc(size, 3);
const stdlib = {
Math: Math,
Float32Array: Float32Array,
Float64Array: Float64Array
};
const f = new external.drom.custom[`fft_f64_${size}_asm`](stdlib, null, heap);
f.init();
addFiltered(suite, 'drom', () => {
f.transform();
});
}
function transform(size) {
const suite = new benchmark.Suite();
addFftjs(suite, size);
addJensNockert(suite, size);
addDSPJS(suite, size);
addDrom(suite, size);
addOouraComplex4(suite, size);
addOouraComplex8(suite, size);
return suite;
}
function addFftjsReal(suite, size) {
const f = new external.fftjs(size);
const input = createInput(f.size);
const data = f.toComplexArray(input);
const out = f.createComplexArray();
addFiltered(suite, 'fft.js', () => {
f.realTransform(out, data);
});
}
function addFourierTransform(suite, size) {
const forward = external.fourierTransform;
const input = createInput(size);
addFiltered(suite, 'fourier-transform', () => {
// It is not exactly fair, since `fourier-transform` also computes
// magnitude. However, when removing this magnitude code locally, fft.js
// outperforms `fourier-transform` by almost the same margin.
forward(input);
});
}
function realTransform(size) {
const suite = new benchmark.Suite();
addFftjsReal(suite, size);
addFourierTransform(suite, size);
addOouraReal4(suite, size);
addOouraReal8(suite, size);
return suite;
}
const benchmarks = [
// { title: 'table construction', suite: construct(16384) },
// { title: 'transform size=64', suite: transform(64) },
// { title: 'transform size=128', suite: transform(128) },
// { title: 'transform size=256', suite: transform(256) },
{ title: 'transform size=512', suite: transform(512) },
{ title: 'transform size=1024', suite: transform(1024) },
{ title: 'transform size=2048', suite: transform(2048) },
// { title: 'transform size=4096', suite: transform(4096) },
// { title: 'transform size=8192', suite: transform(8192) },
// { title: 'transform size=16384', suite: transform(16384) },
// { title: 'transform size=32768', suite: transform(32768) },
// { title: 'transform size=65536', suite: transform(65536) },
{ title: 'realTransform size=2048', suite: realTransform(2048) },
{ title: 'realTransform size=4096', suite: realTransform(4096) },
{ title: 'realTransform size=8192', suite: realTransform(8192) },
// { title: 'realTransform size=16384', suite: realTransform(16384) }
];
/* eslint-disable no- */
benchmarks.forEach((bench) => {
if (bench.title.match(filter0) === null)
return;
console.log('===== %s =====', bench.title);
bench.suite.on('cycle', (event) => {
console.log(' '+ String(event.target));
}).on('complete', function() {
console.log(' Fastest is ' + this.filter('fastest').map('name'));
});
bench.suite.run();
});