Add xorshift to core::rand, which gave a 3x speedup for graph generation in the bfs code. Also, remove trailing white space.

This commit is contained in:
Eric Holk
2012-05-30 16:31:16 -07:00
parent 09a32aedb5
commit ad292a8c73
3 changed files with 34 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
#[doc = "Random number generation"];
export rng, seed, seeded_rng, weighted, extensions;
export xorshift, seeded_xorshift;
enum rctx {}
@@ -253,6 +254,35 @@ fn seeded_rng(seed: [u8]) -> rng {
@rand_res(rustrt::rand_new_seeded(seed)) as rng
}
type xorshift_state = {
mut x: u32,
mut y: u32,
mut z: u32,
mut w: u32
};
impl of rng for xorshift_state {
fn next() -> u32 {
let x = self.x;
let mut t = x ^ (x << 11);
self.x = self.y;
self.y = self.z;
self.z = self.w;
let w = self.w;
self.w = w ^ (w >> 19) ^ (t ^ (t >> 8));
self.w
}
}
fn xorshift() -> rng {
// constants taken from http://en.wikipedia.org/wiki/Xorshift
seeded_xorshift(123456789u32, 362436069u32, 521288629u32, 88675123u32)
}
fn seeded_xorshift(x: u32, y: u32, z: u32, w: u32) -> rng {
{mut x: x, mut y: y, mut z: z, mut w: w} as rng
}
#[cfg(test)]
mod tests {

View File

@@ -21,7 +21,7 @@ type graph = [[node_id]];
type bfs_result = [node_id];
fn make_edges(scale: uint, edgefactor: uint) -> [(node_id, node_id)] {
let r = rand::rng();
let r = rand::xorshift();
fn choose_edge(i: node_id, j: node_id, scale: uint, r: rand::rng)
-> (node_id, node_id) {
@@ -247,12 +247,12 @@ fn pbfs(&&graph: arc::arc<graph>, key: node_id) -> bfs_result {
white {
let i = i as node_id;
let neighbors = (*graph)[i];
let neighbors = graph[i];
let mut color = white;
neighbors.each() {|k|
if is_gray((*colors)[k]) {
if is_gray(colors[k]) {
color = gray(k);
false
}