mac 上asm wasm js 性能对比
以js作为速度单位, 按照计算时间的比值
基于c的wasm 速度是js的2.3倍左右
asm的wasm速度是1.6左右
对比代码
const fs = require("fs");
const Module = require("./a.out")
const loader = require("@assemblyscript/loader");
const imports = { /* imports go here */ };
const wasmModule = loader.instantiateSync(fs.readFileSync(__dirname + "/build/optimized.wasm"), imports);
// const wasmModule = loader.instantiateSync("./build/optimized.wasm", imports);
const fibAsm = wasmModule.exports.fibAsm;
// fibWasm = Module.cwrap("fibWasm", "number", ["number"]);
const fibJs = n => n < 2 ? n : fibJs(n - 1) + fibJs(n - 2)
Module.onRuntimeInitialized = () => {
const fibWasm = Module.cwrap("fibWasm", "number", ["number"]);
console.log("fibWasm", fibWasm(12));
for (let i = 0; i < 10; i++) {
console.log(i, "js asm cpp", fibJs(i), fibAsm(i), fibWasm(i))
}
const size = 40
let st = + new Date()
for (let i = 0; i < size; i++)
fibJs(i)
const jsTime = +new Date() - st
console.log("js", jsTime)
st = + new Date()
for (let i = 0; i < size; i++)
fibAsm(i)
const asmTime = +new Date() - st
console.log("fibAsm", asmTime, asmTime / jsTime, jsTime / asmTime)
st = + new Date()
for (let i = 0; i < size; i++)
fibWasm(i)
const wasmTime = +new Date() - st
console.log("wasm", wasmTime, wasmTime / jsTime, jsTime / wasmTime)
};