问题
I've created a jsPref, to test this asm.js thing: http://jsperf.com/asm-diag
I think I did something wrong, because the asmjs code runs two times slower than the regular js code, even in firefox nightly.
I have no idea what's wrong in the code.
Thanks in advance,
Edit:
Benchmark.prototype.setup = function() {
function DiagModule(stdlib, foreign, heap) {
"use asm";
// Variable Declarations
var sqrt = stdlib.Math.sqrt;
var pow = stdlib.Math.pow;
// Function Declarations
function square(x) {
x = x|0;
return (pow(x, 2))|0;
}
function diag(x, y) {
x = x|0;
y = y|0;
return +sqrt(square(x) + square(y));
}
return { diag: diag };
}
diag = DiagModule({ Math: Math }).diag;
};
asm:
var _diag = diag(10, 100);
regular:
var _diag = Math.sqrt(Math.pow(10, 2) + Math.pow(100, 2))
回答1:
There is a significant overhead when calling asm.js function form JS and the function you're benchmarking doesn't do enough work to make up for the calling overhead.
When you use asm.js functions try to minimize asm<->JS communication and do bigger chunks of work in asm.js modules.
jsperf forces asm.js module to be recompiled several times during the test, but Firefox doesn't support recompilation yet, so jsperf tests never run in asm.js mode.
回答2:
Just stumbled upon this asm.js thing - which sounds awesome. I made a stab at modifying the test slightly to make the situation in the two tests as similar as possible with regard to function calls, property lookup, etc. http://jsperf.com/asm-diag/10.
I guess, a bigger piece of code is needed - heavy math is where it would likely excel. I'm gonna follow asm.js development closely.
来源:https://stackoverflow.com/questions/16688187/why-is-the-asmjs-code-slower-even-in-firefox