Skip to content

Commit 260d74d

Browse files
committed
auto merge of #5695 : bstrie/rust/noise, r=thestinger
With --opt-level=3, this version runs in 0.014s on my machine, compared to 0.252s for the previous version. Mostly due to the #[inline] on get().
2 parents afd5cba + 84e5033 commit 260d74d

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

src/test/bench/noise.rs

+32-29
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
// Perlin noise benchmark from https://gist.github.com/1170424
22

3-
use core::rand::RngUtil;
3+
use core::rand::{Rng, RngUtil};
44

55
struct Vec2 {
66
x: f32,
77
y: f32,
88
}
99

10-
fn lerp(a: f32, b: f32, v: f32) -> f32 { a * (1.0 - v) + b * v }
11-
fn smooth(v: f32) -> f32 { v * v * (3.0 - 2.0 * v) }
10+
#[inline(always)]
11+
fn lerp(a: f32, b: f32, v: f32) -> f32 { a * (1.0 - v) + b * v }
1212

13-
fn random_gradient(r: @rand::Rng) -> Vec2 {
13+
#[inline(always)]
14+
fn smooth(v: f32) -> f32 { v * v * (3.0 - 2.0 * v) }
15+
16+
fn random_gradient(r: @Rng) -> Vec2 {
1417
let v = r.gen_float() * float::consts::pi * 2.0;
15-
Vec2{
18+
Vec2 {
1619
x: float::cos(v) as f32,
1720
y: float::sin(v) as f32,
1821
}
1922
}
2023

2124
fn gradient(orig: Vec2, grad: Vec2, p: Vec2) -> f32 {
22-
let sp = Vec2{x: p.x - orig.x, y: p.y - orig.y};
25+
let sp = Vec2 {x: p.x - orig.x, y: p.y - orig.y};
2326
grad.x * sp.x + grad.y + sp.y
2427
}
2528

@@ -28,28 +31,28 @@ struct Noise2DContext {
2831
permutations: [int, ..256],
2932
}
3033

31-
fn Noise2DContext() -> ~Noise2DContext {
32-
let r = rand::Rng();
33-
let mut rgradients = [ Vec2 { x: 0.0, y: 0.0 }, ..256 ];
34-
for int::range(0, 256) |i| { rgradients[i] = random_gradient(r); }
35-
let mut permutations = [ 0, ..256 ];
36-
for int::range(0, 256) |i| { permutations[i] = i; }
37-
r.shuffle_mut(permutations);
38-
39-
~Noise2DContext{
40-
rgradients: rgradients,
41-
permutations: permutations,
34+
pub impl Noise2DContext {
35+
fn new() -> Noise2DContext {
36+
let r = rand::Rng();
37+
let mut rgradients = [ Vec2 { x: 0.0, y: 0.0 }, ..256 ];
38+
for int::range(0, 256) |i| { rgradients[i] = random_gradient(r); }
39+
let mut permutations = [ 0, ..256 ];
40+
for int::range(0, 256) |i| { permutations[i] = i; }
41+
r.shuffle_mut(permutations);
42+
43+
Noise2DContext {
44+
rgradients: rgradients,
45+
permutations: permutations,
46+
}
4247
}
43-
}
4448

45-
pub impl Noise2DContext {
4649
#[inline(always)]
4750
fn get_gradient(&self, x: int, y: int) -> Vec2 {
4851
let idx = self.permutations[x & 255] + self.permutations[y & 255];
4952
self.rgradients[idx & 255]
5053
}
5154

52-
#[inline(always)]
55+
#[inline]
5356
fn get_gradients(&self, gradients: &mut [Vec2, ..4], origins: &mut [Vec2, ..4], x: f32, y: f32) {
5457
let x0f = f32::floor(x);
5558
let y0f = f32::floor(y);
@@ -63,14 +66,15 @@ pub impl Noise2DContext {
6366
gradients[2] = self.get_gradient(x0, y1);
6467
gradients[3] = self.get_gradient(x1, y1);
6568

66-
origins[0] = Vec2{x: x0f + 0.0, y: y0f + 0.0};
67-
origins[1] = Vec2{x: x0f + 1.0, y: y0f + 0.0};
68-
origins[2] = Vec2{x: x0f + 0.0, y: y0f + 1.0};
69-
origins[3] = Vec2{x: x0f + 1.0, y: y0f + 1.0};
69+
origins[0] = Vec2 {x: x0f + 0.0, y: y0f + 0.0};
70+
origins[1] = Vec2 {x: x0f + 1.0, y: y0f + 0.0};
71+
origins[2] = Vec2 {x: x0f + 0.0, y: y0f + 1.0};
72+
origins[3] = Vec2 {x: x0f + 1.0, y: y0f + 1.0};
7073
}
7174

75+
#[inline]
7276
fn get(&self, x: f32, y: f32) -> f32 {
73-
let p = Vec2{x: x, y: y};
77+
let p = Vec2 {x: x, y: y};
7478
let mut gradients = [ Vec2 { x: 0.0, y: 0.0 }, ..4 ];
7579
let mut origins = [ Vec2 { x: 0.0, y: 0.0 }, ..4 ];
7680
self.get_gradients(&mut gradients, &mut origins, x, y);
@@ -88,9 +92,9 @@ pub impl Noise2DContext {
8892

8993
fn main() {
9094
let symbols = [" ", "░", "▒", "▓", "█", "█"];
91-
let mut pixels = vec::from_elem(256*256, 0f32);
92-
let n2d = Noise2DContext();
93-
for int::range(0, 100) |_| {
95+
let mut pixels = [0f32, ..256*256];
96+
let n2d = ~Noise2DContext::new();
97+
for 100.times {
9498
for int::range(0, 256) |y| {
9599
for int::range(0, 256) |x| {
96100
let v = n2d.get(
@@ -109,4 +113,3 @@ fn main() {
109113
io::println("");
110114
}*/
111115
}
112-

0 commit comments

Comments
 (0)