asm 编写 wasm 对比原生性能

asm 编写 wasm 对比原生性能

tinygo及其不稳定, demo可以, 但是大点的就会出很多奇怪的问题, 今天就遇到没法声明byte数组的问题……………

 

 

https://webassembly.studio/

https://www.assemblyscript.org/quick-start.html

npm install --save @assemblyscript/loader
npm install --save-dev assemblyscript

npx asinit .

npm run asbuild

性能提升不是很明显啊… 还是得试试c

 

 

写法非常简单

 

 

在web中使用

<template>
 
  <button @click="test">test</button>
  <button @click="js">js</button>
  <button @click="wasm">wasm</button>
</template>

<script>
import { ref } from "vue";
import loader from "@assemblyscript/loader";

const WASM_URL = "../build/optimized.wasm";
console.log("loader", loader);
const wasmModule = loader.instantiateStreaming(fetch(WASM_URL));
console.log("wasmModule.exports:", wasmModule);

let fibWasm;
wasmModule.then((obj) => {
  console.log("obj", obj);
  const add = obj.instance.exports.add;
  console.log("add", add(1, 2));
  fibWasm = obj.instance.exports.fib;
});

const fibJs = (n) => (n < 2 ? n : fibJs(n - 1) + fibJs(n - 2));
export default {
  methods: {
    test() {
      console.log("fibWasm", fibWasm);
      let st = 0;
      let ed = 0;
      for (let i = 0; i < 40; i++) {
        st = +new Date();
        let f1 = fibJs(i);
        ed = +new Date();
        let t1 = ed - st;

        st = +new Date();
        let f2 = fibWasm(i);
        ed = +new Date();
        let t2 = ed - st;
        console.log(f1, f2, t1, t2);
      }
    },
    js() {
      let st = +new Date();
      let f2 = fibJs(40);
      let ed = +new Date();
      let t2 = ed - st;
      console.log("js", f2, t2);
    },
    wasm() {
      let st = +new Date();
      let f2 = fibWasm(40);
      let ed = +new Date();
      let t2 = ed - st;
      console.log("wasm", f2, t2);
    },
  },
 
  
};
</script>

 

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » asm 编写 wasm 对比原生性能