Created
August 25, 2019 08:59
-
-
Save yuta0801/d4b2e38547fcd0562ec2c3978247bed6 to your computer and use it in GitHub Desktop.
RustよりJSが早かった時のソースコード
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
var addon = require('../native'); | |
const factorial = (x) => { | |
let res = x; | |
let done = false; | |
while (!done) { | |
x = x - 1; | |
res = res * x; | |
if (x < 2) { | |
done = true; | |
}; | |
}; | |
return res | |
} | |
console.time('rust') | |
for (let i = 1; i <= 10000; i++) { | |
addon.factorial(i) | |
} | |
console.timeEnd('rust') | |
console.time('js') | |
for (let i = 1; i <= 10000; i++) { | |
factorial(i) | |
} | |
console.timeEnd('js') |
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
#[macro_use] | |
extern crate neon; | |
use neon::prelude::*; | |
fn factorial(mut cx: FunctionContext) -> JsResult<JsNumber> { | |
let mut x = cx.argument::<JsNumber>(0)?.value(); | |
let mut res = x; | |
let mut done = false; | |
while !done { | |
x = x - 1.0; | |
res = res * x; | |
if x < 2.0 { | |
done = true; | |
}; | |
}; | |
Ok(cx.number(res)) | |
} | |
register_module!(mut cx, { | |
cx.export_function("factorial", factorial) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment