Created
April 15, 2014 06:48
-
-
Save crongro/10708235 to your computer and use it in GitHub Desktop.
ASM Module performance test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://ejohn.org/blog/asmjs-javascript-compile-target/ | |
function DiagModule(stdlib, foreign, heap) { | |
//"use asm"; | |
// Variable Declarations | |
var sqrt = stdlib.Math.sqrt; | |
// Function Declarations | |
function square(x) { | |
x = +x; | |
return +(x*x); | |
} | |
function diag(x, y) { | |
x = +x; | |
y = +y; | |
return +sqrt(square(x) + square(y)); | |
} | |
return { diag: diag }; | |
} | |
function DiagModuleWithASM(stdlib, foreign, heap) { | |
"use asm"; | |
// Variable Declarations | |
var sqrt = stdlib.Math.sqrt; | |
// Function Declarations | |
function square(x) { | |
x = +x; | |
return +(x*x); | |
} | |
function diag(x, y) { | |
x = +x; | |
y = +y; | |
return +sqrt(square(x) + square(y)); | |
} | |
return { diag: diag }; | |
} | |
var start1 = new Date(); | |
for(var i = 0 ; i < 1000000; i++) { | |
DiagModule(window).diag(3,4); | |
} | |
var end1 = new Date(); | |
var start2 = new Date(); | |
for(var i = 0 ; i < 1000000; i++) { | |
DiagModuleWithASM(window).diag(3,4); | |
} | |
var end2 = new Date(); | |
console.log("no ASM->" , end1.getTime() - start1.getTime()); | |
console.log("ASM ->" , end2.getTime() - start2.getTime()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment