测试用例为执行4x4矩阵乘法1000次,对比wasm和js代码消耗的时间:
PC上测试结果:wasm更快
Android上测试结果:wasm更快
iOS上测试结果:为什么wasm更慢了?
var wasmInstance = null;
var wasmExport = null;
var wasmMemory = null;
function initWasm(cb){
var importObject = {};
var defer = WXWebAssembly.instantiate("mat4_test.wasm", importObject);
defer.then((result)=>{
console.log(result);
wasmInstance = result.instance;
wasmExport = wasmInstance.exports;
wasmMemory = wasmExport.memory;
cb && cb();
}).catch(function(err){
console.error(err.toString());
});
}
function js_multiply(left,right,e){
var l = right;
var r = left;
var l11 = l[0], l12 = l[1], l13 = l[2], l14 = l[3];
var l21 = l[4], l22 = l[5], l23 = l[6], l24 = l[7];
var l31 = l[8], l32 = l[9], l33 = l[10], l34 = l[11];
var l41 = l[12], l42 = l[13], l43 = l[14], l44 = l[15];
var r11 = r[0], r12 = r[1], r13 = r[2], r14 = r[3];
var r21 = r[4], r22 = r[5], r23 = r[6], r24 = r[7];
var r31 = r[8], r32 = r[9], r33 = r[10], r34 = r[11];
var r41 = r[12], r42 = r[13], r43 = r[14], r44 = r[15];
e[0] = (l11 * r11) + (l12 * r21) + (l13 * r31) + (l14 * r41);
e[1] = (l11 * r12) + (l12 * r22) + (l13 * r32) + (l14 * r42);
e[2] = (l11 * r13) + (l12 * r23) + (l13 * r33) + (l14 * r43);
e[3] = (l11 * r14) + (l12 * r24) + (l13 * r34) + (l14 * r44);
e[4] = (l21 * r11) + (l22 * r21) + (l23 * r31) + (l24 * r41);
e[5] = (l21 * r12) + (l22 * r22) + (l23 * r32) + (l24 * r42);
e[6] = (l21 * r13) + (l22 * r23) + (l23 * r33) + (l24 * r43);
e[7] = (l21 * r14) + (l22 * r24) + (l23 * r34) + (l24 * r44);
e[8] = (l31 * r11) + (l32 * r21) + (l33 * r31) + (l34 * r41);
e[9] = (l31 * r12) + (l32 * r22) + (l33 * r32) + (l34 * r42);
e[10] = (l31 * r13) + (l32 * r23) + (l33 * r33) + (l34 * r43);
e[11] = (l31 * r14) + (l32 * r24) + (l33 * r34) + (l34 * r44);
e[12] = (l41 * r11) + (l42 * r21) + (l43 * r31) + (l44 * r41);
e[13] = (l41 * r12) + (l42 * r22) + (l43 * r32) + (l44 * r42);
e[14] = (l41 * r13) + (l42 * r23) + (l43 * r33) + (l44 * r43);
e[15] = (l41 * r14) + (l42 * r24) + (l43 * r34) + (l44 * r44);
}
function doTest(){
var lLoc = wasmExport.stackAlloc(192);
var rLoc = lLoc + 64;
var oLoc = lLoc + 128;
var l = new Float32Array(wasmMemory.buffer, lLoc, 16);
var r = new Float32Array(wasmMemory.buffer, rLoc, 16);
var o = new Float32Array(wasmMemory.buffer, oLoc, 16);
l.set([6.8706560134887695, 0, 0, 0, 0, 2.2422866821289062, -0.7071304321289062, -0.7071068286895752, 0, -2.2422866821289062, -0.7071304321289062, -0.7071068286895752, -35.222686767578125, 35.70803451538086, 100.25936126708984, 100.35601806640625]);
r.set([0.9876883625984192, 0, -0.15643446147441864, 0, 0, 1, 0, 0, 0.15643446147441864, 0, 0.9876883625984192, 0, 4.099999904632568, 0, 14.210000038146973, 1]);
o.set([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
var i;
var st1 = Date.now();
for(i=0; i<10000; i++){
wasmExport.mat4_multiply(lLoc, rLoc, oLoc);
}
var et1 = Date.now();
var st2 = Date.now();
for(i=0; i<10000; i++){
js_multiply(l, r, o);
}
var et2 = Date.now();
console.log("Matrix4x4.multiply use wasm: " + (et1-st1) + "ms");
console.log("Matrix4x4.multiply use js: " + (et2-st2) + "ms");
}
initWasm(doTest);
//=========================== c 代码 =============================
//emcc tests/mat4_test.c -O3 -s WASM=1 --no-entry -o output/mat4_test.wasm -s EXPORTED_FUNCTIONS="_mat4_multiply"
/*矩阵相乘*/
void mat4_multiply(float *l, float *r, float *e){
float l11 = r[0], l12 = r[1], l13 = r[2], l14 = r[3];
float l21 = r[4], l22 = r[5], l23 = r[6], l24 = r[7];
float l31 = r[8], l32 = r[9], l33 = r[10], l34 = r[11];
float l41 = r[12], l42 = r[13], l43 = r[14], l44 = r[15];
float r11 = l[0], r12 = l[1], r13 = l[2], r14 = l[3];
float r21 = l[4], r22 = l[5], r23 = l[6], r24 = l[7];
float r31 = l[8], r32 = l[9], r33 = l[10], r34 = l[11];
float r41 = l[12], r42 = l[13], r43 = l[14], r44 = l[15];
e[0] = l11 * r11 + l12 * r21 + l13 * r31 + l14 * r41;
e[1] = l11 * r12 + l12 * r22 + l13 * r32 + l14 * r42;
e[2] = l11 * r13 + l12 * r23 + l13 * r33 + l14 * r43;
e[3] = l11 * r14 + l12 * r24 + l13 * r34 + l14 * r44;
e[4] = l21 * r11 + l22 * r21 + l23 * r31 + l24 * r41;
e[5] = l21 * r12 + l22 * r22 + l23 * r32 + l24 * r42;
e[6] = l21 * r13 + l22 * r23 + l23 * r33 + l24 * r43;
e[7] = l21 * r14 + l22 * r24 + l23 * r34 + l24 * r44;
e[8] = l31 * r11 + l32 * r21 + l33 * r31 + l34 * r41;
e[9] = l31 * r12 + l32 * r22 + l33 * r32 + l34 * r42;
e[10] = l31 * r13 + l32 * r23 + l33 * r33 + l34 * r43;
e[11] = l31 * r14 + l32 * r24 + l33 * r34 + l34 * r44;
e[12] = l41 * r11 + l42 * r21 + l43 * r31 + l44 * r41;
e[13] = l41 * r12 + l42 * r22 + l43 * r32 + l44 * r42;
e[14] = l41 * r13 + l42 * r23 + l43 * r33 + l44 * r43;
e[15] = l41 * r14 + l42 * r24 + l43 * r34 + l44 * r44;
}
参考 https://developers.weixin.qq.com/community/develop/doc/000cac2319cbd8ecdc7f062eb53400?_at=1685424828262
麻烦官方回答一下