Update nbody example for Rust (#528)

This commit is contained in:
Max Graey 2019-03-06 22:27:54 +02:00 committed by Daniel Wirtz
parent f841f0fe1f
commit 5f547131ab
6 changed files with 18 additions and 14 deletions

View File

@ -32,16 +32,18 @@ Benchmark
***Environment:*** ***Environment:***
- MacBook Pro (Retina, 15-inch, Late 2013) - MacBook Pro (Retina, 15-inch, Late 2013)
- macOS 10.14.3 - macOS 10.14.3
- node.js v11.9.0 - node.js v11.10.1
- rustc 1.33.0-nightly (ceb251214 2019-01-16) - rustc 1.35.0-nightly (a9da8fc9c 2019-03-04)
***Results:*** ***Results:***
| Target | Time, ***ms*** | Size, ***KB*** | | Target | Time, ***ms*** | Size, ***KB*** |
|-------------------------|-----------------|----------------| |-------------------------|-----------------|----------------|
| **AssemblyScript WASM** | **2901** | **2** | | **AssemblyScript WASM** | **2921** | **2** |
| AssemblyScript ASMJS | 3720 | 19* | | AssemblyScript ASMJS | 3807 | 19* |
| JavaScript | 2716 | 5* | | JavaScript | 2757 | 5* |
| Rust WASM | 2883 | 13 | | Rust WASM | 2866 | 20 / 2** |
___* unminified___ ___* unminified___
___** after wasm-gc___

BIN
examples/n-body/build/rust.optimized.wasm Executable file → Normal file

Binary file not shown.

View File

@ -1,3 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]] [[package]]
name = "rust_nbody" name = "rust_nbody"
version = "0.1.0" version = "0.1.0"

View File

@ -1,7 +1,7 @@
### Build ### Build
```bash ```bash
cargo build --release --target=wasm32-unknown-unknown cargo +nightly build --release --target wasm32-unknown-unknown
``` ```
***Next step optimize target wasm via wasm-opt.*** ***Next step optimize target wasm via wasm-gc***

View File

@ -8,8 +8,8 @@ const compiled = new WebAssembly.Module(
const imports = { const imports = {
env: { env: {
memory: new WebAssembly.Memory({ initial: 17 }), memory: new WebAssembly.Memory({ initial: 17 }),
abort: (filename, line, column) => { abort: (_filename, line, column) => {
throw Error("abort called at " + line + ":" + colum); throw Error("abort called at " + line + ":" + column);
} }
} }
}; };

View File

@ -1,6 +1,6 @@
// Code adopted from https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/nbody-rust-1.html // Code adopted from https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/nbody-rust-1.html
#![feature(core_intrinsics, panic_implementation)] #![feature(core_intrinsics)]
#![no_std] #![no_std]
use core::intrinsics; use core::intrinsics;
@ -156,18 +156,18 @@ fn offset_momentum(bodies: &mut [Planet; N_BODIES]) {
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn init() { pub unsafe extern fn init() {
offset_momentum(&mut BODIES); offset_momentum(&mut BODIES);
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn step() -> f64 { pub unsafe extern fn step() -> f64 {
advance(&mut BODIES, 0.01); advance(&mut BODIES, 0.01);
energy(&BODIES) energy(&BODIES)
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn bench(steps: i32) { pub unsafe extern fn bench(steps: i32) {
for _ in 0..steps { for _ in 0..steps {
advance(&mut BODIES, 0.01); advance(&mut BODIES, 0.01);
} }