问题描述
我已经创建了一个jsPref来测试这个asm.js的东西:
I've created a jsPref, to test this asm.js thing: http://jsperf.com/asm-diag
我认为我做错了什么,因为asmjs代码的运行速度比普通的js代码慢两倍,即使在每晚的firefox中也是如此。
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.
提前致谢,
编辑:
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);
常规:
var _diag = Math.sqrt(Math.pow(10, 2) + Math.pow(100, 2))
推荐答案
-
调用asm.js函数形式的JS和你正在进行基准测试的函数时会有很大的开销做足够的工作来弥补调用开销。
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.
当你使用asm.js函数时,尽量减少asm< - > JS通信并做更大的工作在asm.js模块中。
When you use asm.js functions try to minimize asm<->JS communication and do bigger chunks of work in asm.js modules.
jsperf强制asm.js模块在测试期间重新编译几次,但是Firefox不支持重新编译,所以jsperf测试永远不会在asm.js模式下运行。
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.
这篇关于为什么即使在firefox中asmjs代码也会变慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!