Created
August 17, 2018 05:30
-
-
Save mstruebing/4cf65b928209328a22016a5130b6503f to your computer and use it in GitHub Desktop.
wasm
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script> | |
const fib = n => { | |
switch (n) { | |
case 0: | |
return 0; | |
break; | |
case 1: | |
case 2: | |
return 1; | |
break; | |
default: | |
return fib(n - 1) + fib(n - 2); | |
} | |
}; | |
request = new XMLHttpRequest(); | |
request.open('GET', 'hello_world.gc.wasm'); | |
request.responseType = 'arraybuffer'; | |
request.send(); | |
request.onload = function() { | |
const bytes = request.response; | |
WebAssembly.instantiate(bytes).then(results => { | |
const instance = results.instance; | |
console.log('measuring performance of recursive fib(50)'); | |
let t0 = performance.now(); | |
fib(50); | |
let t1 = performance.now(); | |
console.log("javascript version took: " + (t1 - t0) + " milliseconds.") | |
t0 = performance.now(); | |
instance.exports.fib(50); | |
t1 = performance.now(); | |
console.log("rust version took: " + (t1 - t0) + " milliseconds.") | |
}); | |
}; | |
</script> | |
</head> | |
<body></body> | |
</html> |
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
#![feature(custom_attribute)] | |
#![feature(repr_transparent)] | |
use std::ffi::CString; | |
use std::os::raw::c_char; | |
#[wasm_bindgen] | |
#[no_mangle] | |
pub fn fib(n: i32) -> i32{ | |
match n { | |
0 => 0, | |
1 => 1, | |
_ => fib(n-1) + fib(n-2), | |
} | |
} |
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
build: | |
cargo +nightly build --target wasm32-unknown-unknown --release | |
wasm-gc target/wasm32-unknown-unknown/release/hello_world.wasm -o hello_world.gc.wasm | |
install: | |
rustup update | |
rustup toolchain install nightly | |
rustup target add wasm32-unknown-unknown --toolchain nightly |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment