Skip to content

Commit

Permalink
add game of life benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Mar 28, 2018
1 parent 1758509 commit 67e1f01
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions rayon-demo/src/life/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ fn generations(b: &mut ::test::Bencher) {
fn parallel_generations(b: &mut ::test::Bencher) {
b.iter(|| super::parallel_generations(Board::new(200, 200).random(), 100));
}

#[bench]
fn as_parallel_generations(b: &mut ::test::Bencher) {
b.iter(|| super::as_parallel_generations(Board::new(200, 200).random(), 100));
}
19 changes: 19 additions & 0 deletions rayon-demo/src/life/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use time;

use docopt::Docopt;
use rayon::prelude::*;
use rayon::iter::AsParallel;

#[cfg(test)]
mod bench;
Expand Down Expand Up @@ -93,6 +94,15 @@ impl Board {
self.next_board(new_brd)
}

pub fn as_parallel_next_generation(&self) -> Board {
let new_brd = (0..self.len())
.as_parallel()
.map(|cell| self.successor_cell(cell))
.collect();

self.next_board(new_brd)
}

fn cell_live(&self, x: usize, y: usize) -> bool {
!(x >= self.cols || y >= self.rows) && self.board[y * self.cols + x]
}
Expand Down Expand Up @@ -145,6 +155,11 @@ fn parallel_generations(board: Board, gens: usize) {
for _ in 0..gens { brd = brd.parallel_next_generation(); }
}

fn as_parallel_generations(board: Board, gens: usize) {
let mut brd = board;
for _ in 0..gens { brd = brd.as_parallel_next_generation(); }
}

fn measure(f: fn(Board, usize) -> (), args: &Args) -> u64 {
let (n, gens) = (args.flag_size, args.flag_gens);
let brd = Board::new(n, n).random();
Expand All @@ -168,5 +183,9 @@ pub fn main(args: &[String]) {
let parallel = measure(parallel_generations, &args);
println!("parallel: {:10} ns -> {:.2}x speedup", parallel,
serial as f64 / parallel as f64);

let as_parallel = measure(as_parallel_generations, &args);
println!("parallel: {:10} ns -> {:.2}x speedup", parallel,
serial as f64 / as_parallel as f64);
}
}

0 comments on commit 67e1f01

Please sign in to comment.