Skip to content

Commit 1588ae2

Browse files
committed
bench: Update shootout-nbody for style
1 parent 93dcb9f commit 1588ae2

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

src/test/bench/shootout-nbody.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
// based on:
22
// http://shootout.alioth.debian.org/u32/benchmark.php?test=nbody&lang=java
33

4-
#[abi = "cdecl"]
4+
use std;
5+
6+
// Using sqrt from the standard library is way slower than using libc
7+
// directly even though std just calls libc, I guess it must be
8+
// because the the indirection through another dynamic linker
9+
// stub. Kind of shocking. Might be able to make it faster still with
10+
// an llvm intrinsic.
511
#[nolink]
6-
native mod llvm {
12+
native mod libc {
713
fn sqrt(n: float) -> float;
814
}
915

10-
fn main() {
11-
//
12-
// Leave these commented out to
13-
// finish in a reasonable time
14-
// during 'make check' under valgrind
15-
// 5000000
16-
// 50000000
17-
let inputs: [int] = [50000, 500000];
18-
16+
fn main(args: [str]) {
17+
let n = if vec::len(args) == 2u {
18+
int::from_str(args[1])
19+
} else {
20+
1000000
21+
};
1922
let bodies: [Body::props] = NBodySystem::MakeNBodySystem();
20-
21-
22-
for n: int in inputs {
23-
log(debug, NBodySystem::energy(bodies));
24-
25-
let i: int = 0;
26-
while i < n { NBodySystem::advance(bodies, 0.01); i += 1; }
27-
log(debug, NBodySystem::energy(bodies));
28-
}
23+
std::io::println(#fmt("%f", NBodySystem::energy(bodies)));
24+
let i: int = 0;
25+
while i < n { NBodySystem::advance(bodies, 0.01); i += 1; }
26+
std::io::println(#fmt("%f", NBodySystem::energy(bodies)));
2927
}
3028

3129
// Body::props is a record of floats, so
@@ -79,7 +77,7 @@ mod NBodySystem {
7977

8078
let dSquared: float = dx * dx + dy * dy + dz * dz;
8179

82-
let distance: float = llvm::sqrt(dSquared);
80+
let distance: float = libc::sqrt(dSquared);
8381
let mag: float = dt / (dSquared * distance);
8482

8583
bi.vx -= dx * bj.mass * mag;
@@ -117,7 +115,7 @@ mod NBodySystem {
117115
dy = bodies[i].y - bodies[j].y;
118116
dz = bodies[i].z - bodies[j].z;
119117

120-
distance = llvm::sqrt(dx * dx + dy * dy + dz * dz);
118+
distance = libc::sqrt(dx * dx + dy * dy + dz * dz);
121119
e -= bodies[i].mass * bodies[j].mass / distance;
122120

123121
j += 1;

0 commit comments

Comments
 (0)