add growing benchmark

This commit is contained in:
Crispy 2023-07-21 18:09:17 +02:00
parent 3506e0057f
commit 83ecee6509
5 changed files with 49 additions and 18 deletions

View file

@ -9,7 +9,7 @@ edition = "2021"
rand = "0.8.5"
[[bench]]
name = "large_grid"
name = "main"
harness = false
[dev-dependencies]

View file

@ -1,17 +0,0 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use gol_bitwise::region::Region;
fn large_grid(c: &mut Criterion) {
const SIZE: usize = 2048 / gol_bitwise::tile::WIDTH; // same number of cells regardless of tile size
let mut region = black_box(Region::new(SIZE, SIZE));
region.randomise();
c.bench_function("large grid", |b| {
b.iter(|| {
region.step();
})
});
}
criterion_group!(benches, large_grid);
criterion_main!(benches);

33
benches/main.rs Normal file
View file

@ -0,0 +1,33 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use gol_bitwise::{
region::Region,
tile::{self, Tile},
};
fn large_grid(c: &mut Criterion) {
const SIZE: usize = 2048 / tile::WIDTH; // same number of cells regardless of tile size
let mut region = black_box(Region::new(SIZE, SIZE));
region.randomise();
c.bench_function("large grid", |b| {
b.iter(|| {
region.step();
})
});
}
fn growing(c: &mut Criterion) {
c.bench_function("growing", |b| {
b.iter(|| {
let mut region = black_box(Region::new(1, 1));
region.set_tile(Tile::gliders(), 0, 0);
for _ in 0..1024 {
region.step();
region.auto_grow();
}
})
});
}
criterion_group!(benches, large_grid, growing);
criterion_main!(benches);

View file

@ -19,6 +19,10 @@ impl Region {
}
}
pub fn set_tile(&mut self, tile: Tile, x: usize, y: usize) {
self.tiles[y][x] = tile;
}
pub fn height(&self) -> usize {
self.tiles.len()
}

View file

@ -27,6 +27,17 @@ impl Tile {
Self::EMPTY
}
pub fn gliders() -> Self {
let mut t = Self::EMPTY;
t.rows[1] = 0b1110000;
t.rows[2] = 0b1000000;
t.rows[3] = 0b0100000;
t.rows[WIDTH - 4] = 0b0100;
t.rows[WIDTH - 3] = 0b0010;
t.rows[WIDTH - 2] = 0b1110;
t
}
pub fn edge_east(&self) -> Row {
let mut edge = 0;
for n in 0..WIDTH {