Last active
February 7, 2022 20:15
-
-
Save dy/ca43999d6ed1a4a3d3773a71178c51fa to your computer and use it in GitHub Desktop.
WASM array fill performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; test passing array to and back | |
| (module | |
| (import "js" "mem" (memory 1)) | |
| (import "console" "log" (func $log (param i32 f32))) | |
| (global $blockSize (import "js" "blockSize") (mut i32)) | |
| (global $len (mut i32) (i32.const 0)) | |
| ;; len = blockSize * 4 | |
| (func (global.set $len (i32.mul (global.get $blockSize) (i32.const 4)))) | |
| (start 1) | |
| ;; gain processing function | |
| (func $amp (export "amp") (param $amp f32) | |
| (local $i i32) | |
| (local $x f32) | |
| (loop $gain | |
| ;; x = input[i] | |
| (local.set $x (f32.load (local.get $i))) | |
| ;; console.log(i, x) | |
| ;; (call $log (local.get $i) (local.get $x)) | |
| ;; x = x * amp | |
| (local.set $x (f32.mul (local.get $x) (local.get $amp))) | |
| ;; input[i] = x * amp | |
| (f32.store (local.get $i) (local.get $x)) | |
| ;; i++ | |
| (local.set $i (i32.add (local.get $i) (i32.const 4))) | |
| ;; if (i < len) repeat | |
| (br_if $gain (i32.lt_s (local.get $i) (global.get $len))) | |
| ) | |
| ) | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const BLOCK = 1024 | |
| var buf = fs.readFileSync('./array.wasm') | |
| const mod = new WebAssembly.Module(buf) | |
| const mem = new WebAssembly.Memory({ initial:1 }) | |
| const blockSize = new WebAssembly.Global({value: 'i32', mutable: true}, BLOCK) | |
| // blockSize.value | |
| const f32mem = new Float32Array(mem.buffer) | |
| const importObj = {js: {mem, blockSize}, console:{log(a,b){console.log(a,b)}}} | |
| const instance = new WebAssembly.Instance(mod, importObj) | |
| const {amp} = instance.exports | |
| let src = Array.from({length:BLOCK}, (a,i)=>i) | |
| f32mem.set(src) | |
| console.time('wasm amp') | |
| for (let i = 0; i < MAX; i++) amp(.5) | |
| console.timeEnd('wasm amp') | |
| console.time('js amp') | |
| for (let i = 0; i < MAX; i++) for (let j = 0; j < src.length; j++) src[j]*=.5 | |
| console.timeEnd('js amp') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment