Skip to content

Instantly share code, notes, and snippets.

@walf443
Last active August 29, 2015 14:26
Show Gist options
  • Save walf443/09d0d0136021d6bf903c to your computer and use it in GitHub Desktop.
Save walf443/09d0d0136021d6bf903c to your computer and use it in GitHub Desktop.
benchmark SIMD.js
function benchmark(title, iteration, callback) { start = performance.now(); for (i = 0; i < iteration; i++ ) { callback(); }; end = performance.now(); console.log("%s: time: %f, average: %f", title, end - start, ( end - start ) / iteration); }
function benchmarkSIMD(iteration) {
console.log(navigator.userAgent);
let a = SIMD.Float32x4(1, 2, 3, 4);
let b = SIMD.Float32x4(2,3,4,5);
let aa = [1,2,3,4];
let bb = [2,3,4,5];
benchmark("add SIMD", iteration, function() { return SIMD.Float32x4.add(a, b) });
benchmark("add", iteration, function() {
r0 = aa[0] + bb[0];
r1 = aa[1] + bb[1];
r2 = aa[2] + bb[2];
r3 = aa[3] + bb[3];
return [r0, r1, r2, r3];
});
benchmark("sub SIMD", iteration, function() { return SIMD.Float32x4.sub(b, a) });
benchmark("sub", iteration, function() {
r0 = bb[0] - aa[0];
r1 = bb[1] - aa[1];
r2 = bb[2] - aa[2];
r3 = bb[3] - aa[3];
return [r0, r1, r2, r3];
});
benchmark("sub", iteration, function() { return [bb[0] - aa[0], bb[1] - aa[1], bb[2] - aa[2], bb[3] - aa[3]] });
benchmark("mul SIMD", iteration, function() { return SIMD.Float32x4.mul(a, b) });
benchmark("mul", iteration, function() {
r0 = aa[0] * bb[0];
r1 = aa[1] * bb[1];
r2 = aa[2] * bb[2];
r3 = aa[3] * bb[3];
return [r0, r1, r2, r3];
});
benchmark("div SIMD", iteration, function() { return SIMD.Float32x4.div(b, a) });
benchmark("div", iteration, function() {
r0 = bb[0] / aa[0];
r1 = bb[1] / aa[1];
r2 = bb[2] / aa[2];
r3 = bb[3] / aa[3];
return [r0, r1, r2, r3];
});
}
@walf443
Copy link
Author

walf443 commented Jul 28, 2015

add SIMD: time: 6.355000, average: 0.000635
add: time: 4.360000, average: 0.000436
sub SIMD: time: 7.180000, average: 0.000718
sub: time: 5.895000, average: 0.000590
mul SIMD: time: 5.985000, average: 0.000598
mul: time: 4.420000, average: 0.000442
div SIMD: time: 6.760000, average: 0.000676
div: time: 3.420000, average: 0.000342

@walf443
Copy link
Author

walf443 commented Jul 28, 2015

Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:42.0) Gecko/20100101 Firefox/42.0
add SIMD: time: 8.725000, average: 0.000873
add: time: 17.170000, average: 0.001717
sub SIMD: time: 7.970000, average: 0.000797
sub: time: 16.155000, average: 0.001615
sub: time: 4.205000, average: 0.000420
mul SIMD: time: 6.225000, average: 0.000622
mul: time: 14.355000, average: 0.001436
div SIMD: time: 5.265000, average: 0.000527
div: time: 13.820000, average: 0.001382
div: time: 4.425000, average: 0.000443

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment