Created
February 11, 2020 19:04
-
-
Save petersalomonsen/ea151e7fee1a237d18f13b1b8ab68d01 to your computer and use it in GitHub Desktop.
sin vs math.sin benchmark
This file contains 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
./node_modules/.bin/asc -O3 --use Math=JSMath --runtime none test.ts -o test.wasm | |
node test.js |
This file contains 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
async function test() { | |
const mod = await WebAssembly.instantiate(require('fs').readFileSync('test.wasm'), { | |
Math: Math | |
}); | |
const count = 500000; | |
const inc = (Math.PI / 180); | |
let start = new Date().getTime(); | |
for(var n=0;n<count * Math.PI;n+=inc) { | |
mod.instance.exports.mathsin(n); | |
} | |
console.log('mathsin', (new Date().getTime()-start)); | |
start = new Date().getTime(); | |
for(var n=0;n<count * Math.PI;n+=inc) { | |
mod.instance.exports.sin(n); | |
} | |
console.log('sin', (new Date().getTime()-start)); | |
} | |
test(); |
This file contains 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
export const PI: f32 = 3.141592653589793; | |
export function sin(x: f32): f32 { | |
// By Max Graey ( https://github.com/petersalomonsen/javascriptmusic/issues/2#issuecomment-469419609 ) | |
var y: f32, z: f32; | |
x *= 1 / PI; | |
y = floor(x); | |
z = x - y; | |
z *= 1.0 - z; | |
z *= 3.6 * z + 3.1; | |
return select(-z, z, <i32>y & 1); | |
} | |
export function mathsin(val: f64): f64 { | |
return Math.sin(val); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment