gpt4 book ai didi

javascript - 为什么即使在 Firefox 中 asmjs 代码也比较慢?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:25:57 24 4
gpt4 key购买 nike

我创建了一个 jsPref 来测试这个 asm.js 东西:http://jsperf.com/asm-diag

我想我做错了什么,因为 asmjs 代码的运行速度比常规 js 代码慢两倍,即使在 firefox nightly 中也是如此。

我不知道代码中有什么问题。

提前致谢


编辑:

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;
};

汇编:

var _diag = diag(10, 100);

常规:

var _diag = Math.sqrt(Math.pow(10, 2) + Math.pow(100, 2))

最佳答案

  1. 从 JS 调用 asm.js 函数时会产生很大的开销,而您要进行基准测试的函数没有做足够的工作来弥补调用开销。

    当您使用 asm.js 函数时,尽量减少 asm<->JS 通信并在 asm.js 模块中做更大的工作。

  2. jsperf 在测试期间强制 asm.js 模块重新编译多次,但 Firefox 尚不支持重新编译,因此 jsperf 测试永远不会在 asm.js 模式下运行。

关于javascript - 为什么即使在 Firefox 中 asmjs 代码也比较慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16688187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com