Added benchmarks to find the most appropriate data structure

This commit is contained in:
Romain 2023-09-16 09:28:05 +02:00
parent 9bb2f4c20d
commit 2d22de28b9
3 changed files with 45 additions and 4 deletions

View File

@ -10,4 +10,10 @@ bytemuck = "1.14.0"
env_logger = "0.10.0"
pollster = "0.3.0"
wgpu = "0.17.0"
winit = "0.28.6"
winit = "0.28.6"
criterion = { version = "0.4", features = ["html_reports"] }
[[bench]]
name = "voxel_map_benchmark"
harness = false

View File

@ -0,0 +1,37 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
const MAP_WIDTH: usize = 100;
const MAP_HEIGHT: usize = 100;
const MAP_DEPTH: usize = 100;
#[derive(Clone, Copy, Debug)]
struct Voxel {
r: u8, g: u8, b: u8
}
fn voxel_vec_1d(data: Vec<Voxel>) -> Voxel {
const X: usize = 0;
const Y: usize = 0;
const Z: usize = 0;
return data[X + Y * MAP_WIDTH + Z * MAP_WIDTH * MAP_HEIGHT];
}
fn voxel_vec_3d(data: Vec<Vec<Vec<Voxel>>>) -> Voxel {
const X: usize = 0;
const Y: usize = 0;
const Z: usize = 0;
return data[X][Y][Z];
}
fn criterion_benchmark(c: &mut Criterion) {
let vec_1d = vec![Voxel { r: 0, g: 0, b: 0 }; MAP_WIDTH * MAP_HEIGHT * MAP_DEPTH];
let vec_3d = vec![vec![vec![Voxel { r: 0, g: 0, b: 0 }; MAP_DEPTH]; MAP_HEIGHT]; MAP_WIDTH];
c.bench_function("voxels in 1d vector", |b| b.iter(|| voxel_vec_1d(black_box(vec_1d.clone()))));
c.bench_function("voxels in 3d vector", |b| b.iter(|| voxel_vec_3d(black_box(vec_3d.clone()))));
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

View File

@ -1,8 +1,6 @@
#[derive(PartialEq, Clone, Copy, Debug)]
struct Voxel {
r: u8,
g: u8,
b: u8
r: u8, g: u8, b: u8
}
const AIR_VOXEL: Voxel = Voxel { r: 0, g: 0, b: 0 };