Skip to content

Make bench/noise.rs more idiomatic and 20x faster #5695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 32 additions & 29 deletions src/test/bench/noise.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
// Perlin noise benchmark from https://gist.github.com/1170424

use core::rand::RngUtil;
use core::rand::{Rng, RngUtil};

struct Vec2 {
x: f32,
y: f32,
}

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

fn random_gradient(r: @rand::Rng) -> Vec2 {
#[inline(always)]
fn smooth(v: f32) -> f32 { v * v * (3.0 - 2.0 * v) }

fn random_gradient(r: @Rng) -> Vec2 {
let v = r.gen_float() * float::consts::pi * 2.0;
Vec2{
Vec2 {
x: float::cos(v) as f32,
y: float::sin(v) as f32,
}
}

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

Expand All @@ -28,28 +31,28 @@ struct Noise2DContext {
permutations: [int, ..256],
}

fn Noise2DContext() -> ~Noise2DContext {
let r = rand::Rng();
let mut rgradients = [ Vec2 { x: 0.0, y: 0.0 }, ..256 ];
for int::range(0, 256) |i| { rgradients[i] = random_gradient(r); }
let mut permutations = [ 0, ..256 ];
for int::range(0, 256) |i| { permutations[i] = i; }
r.shuffle_mut(permutations);

~Noise2DContext{
rgradients: rgradients,
permutations: permutations,
pub impl Noise2DContext {
fn new() -> Noise2DContext {
let r = rand::Rng();
let mut rgradients = [ Vec2 { x: 0.0, y: 0.0 }, ..256 ];
for int::range(0, 256) |i| { rgradients[i] = random_gradient(r); }
let mut permutations = [ 0, ..256 ];
for int::range(0, 256) |i| { permutations[i] = i; }
r.shuffle_mut(permutations);

Noise2DContext {
rgradients: rgradients,
permutations: permutations,
}
}
}

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

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

origins[0] = Vec2{x: x0f + 0.0, y: y0f + 0.0};
origins[1] = Vec2{x: x0f + 1.0, y: y0f + 0.0};
origins[2] = Vec2{x: x0f + 0.0, y: y0f + 1.0};
origins[3] = Vec2{x: x0f + 1.0, y: y0f + 1.0};
origins[0] = Vec2 {x: x0f + 0.0, y: y0f + 0.0};
origins[1] = Vec2 {x: x0f + 1.0, y: y0f + 0.0};
origins[2] = Vec2 {x: x0f + 0.0, y: y0f + 1.0};
origins[3] = Vec2 {x: x0f + 1.0, y: y0f + 1.0};
}

#[inline]
fn get(&self, x: f32, y: f32) -> f32 {
let p = Vec2{x: x, y: y};
let p = Vec2 {x: x, y: y};
let mut gradients = [ Vec2 { x: 0.0, y: 0.0 }, ..4 ];
let mut origins = [ Vec2 { x: 0.0, y: 0.0 }, ..4 ];
self.get_gradients(&mut gradients, &mut origins, x, y);
Expand All @@ -88,9 +92,9 @@ pub impl Noise2DContext {

fn main() {
let symbols = [" ", "░", "▒", "▓", "█", "█"];
let mut pixels = vec::from_elem(256*256, 0f32);
let n2d = Noise2DContext();
for int::range(0, 100) |_| {
let mut pixels = [0f32, ..256*256];
let n2d = ~Noise2DContext::new();
for 100.times {
for int::range(0, 256) |y| {
for int::range(0, 256) |x| {
let v = n2d.get(
Expand All @@ -109,4 +113,3 @@ fn main() {
io::println("");
}*/
}