Add must_use and cleanup benchmark
This commit is contained in:
parent
f28b3a3281
commit
4aa88f4a65
2 changed files with 23 additions and 31 deletions
|
@ -3,13 +3,13 @@ use grid::grid;
|
||||||
use grid::Grid;
|
use grid::Grid;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
const SIZE: usize = 1000;
|
const SIZE: usize = 1_000;
|
||||||
|
|
||||||
fn init_vec_vec() -> Vec<Vec<u64>> {
|
fn init_vec_vec() -> Vec<Vec<u8>> {
|
||||||
vec![vec![0; SIZE]; SIZE]
|
vec![vec![0; SIZE]; SIZE]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_grid() -> Grid<u64> {
|
fn init_grid() -> Grid<u8> {
|
||||||
Grid::init(SIZE, SIZE, 0)
|
Grid::init(SIZE, SIZE, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,9 +17,6 @@ fn criterion_benchmark(c: &mut Criterion) {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let mut rand = || rng.gen_range(0..SIZE);
|
let mut rand = || rng.gen_range(0..SIZE);
|
||||||
|
|
||||||
let mut rng_u64 = rand::thread_rng();
|
|
||||||
let mut rand_u64 = || rng_u64.gen::<u64>();
|
|
||||||
|
|
||||||
// Init macro
|
// Init macro
|
||||||
c.bench_function("vecvec_init_macro", |b| {
|
c.bench_function("vecvec_init_macro", |b| {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
|
@ -37,16 +34,6 @@ fn criterion_benchmark(c: &mut Criterion) {
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
c.bench_function("vec_init_macro", |b| {
|
|
||||||
b.iter(|| {
|
|
||||||
vec![
|
|
||||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7,
|
|
||||||
8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
|
|
||||||
6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3,
|
|
||||||
4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
||||||
]
|
|
||||||
})
|
|
||||||
});
|
|
||||||
c.bench_function("grid_init_macro", |b| {
|
c.bench_function("grid_init_macro", |b| {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
grid![[0,1,2,3,4,5,6,7,8,9]
|
grid![[0,1,2,3,4,5,6,7,8,9]
|
||||||
|
@ -101,11 +88,7 @@ fn criterion_benchmark(c: &mut Criterion) {
|
||||||
b.iter_batched(
|
b.iter_batched(
|
||||||
|| (rand(), rand()),
|
|| (rand(), rand()),
|
||||||
|(x, y)| {
|
|(x, y)| {
|
||||||
let _v = vec_vec
|
let _v = vec_vec.get(x).unwrap().get(y).unwrap();
|
||||||
.get(x)
|
|
||||||
.unwrap()
|
|
||||||
.get(y)
|
|
||||||
.unwrap();
|
|
||||||
},
|
},
|
||||||
criterion::BatchSize::SmallInput,
|
criterion::BatchSize::SmallInput,
|
||||||
)
|
)
|
||||||
|
@ -115,9 +98,7 @@ fn criterion_benchmark(c: &mut Criterion) {
|
||||||
b.iter_batched(
|
b.iter_batched(
|
||||||
|| (rand(), rand()),
|
|| (rand(), rand()),
|
||||||
|(x, y)| {
|
|(x, y)| {
|
||||||
let _v = grid
|
let _v = grid.get(x, y).unwrap();
|
||||||
.get(x, y)
|
|
||||||
.unwrap();
|
|
||||||
},
|
},
|
||||||
criterion::BatchSize::SmallInput,
|
criterion::BatchSize::SmallInput,
|
||||||
)
|
)
|
||||||
|
@ -126,11 +107,19 @@ fn criterion_benchmark(c: &mut Criterion) {
|
||||||
//Set
|
//Set
|
||||||
c.bench_function("vecvec_set", |b| {
|
c.bench_function("vecvec_set", |b| {
|
||||||
let mut vec_vec = init_vec_vec();
|
let mut vec_vec = init_vec_vec();
|
||||||
b.iter(|| vec_vec[rand()][rand()] = rand_u64())
|
b.iter_batched(
|
||||||
|
|| (rand(), rand()),
|
||||||
|
|(x, y)| vec_vec[x][y] = 42,
|
||||||
|
criterion::BatchSize::SmallInput,
|
||||||
|
)
|
||||||
});
|
});
|
||||||
c.bench_function("gird_set", |b| {
|
c.bench_function("grid_set", |b| {
|
||||||
let mut gird = init_grid();
|
let mut g = init_grid();
|
||||||
b.iter(|| gird[rand()][rand()] = rand_u64())
|
b.iter_batched(
|
||||||
|
|| (rand(), rand()),
|
||||||
|
|(x, y)| g[x][y] = 42,
|
||||||
|
criterion::BatchSize::SmallInput,
|
||||||
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
// Push
|
// Push
|
||||||
|
|
|
@ -220,6 +220,7 @@ impl<T> Grid<T> {
|
||||||
///
|
///
|
||||||
/// Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.
|
/// Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
pub unsafe fn get_unchecked(&self, row: usize, col: usize) -> &T {
|
pub unsafe fn get_unchecked(&self, row: usize, col: usize) -> &T {
|
||||||
self.data.get_unchecked(row * self.cols + col)
|
self.data.get_unchecked(row * self.cols + col)
|
||||||
}
|
}
|
||||||
|
@ -231,13 +232,14 @@ impl<T> Grid<T> {
|
||||||
///
|
///
|
||||||
/// Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.
|
/// Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
pub unsafe fn get_unchecked_mut(&mut self, row: usize, col: usize) -> &mut T {
|
pub unsafe fn get_unchecked_mut(&mut self, row: usize, col: usize) -> &mut T {
|
||||||
let cols = self.cols;
|
self.data.get_unchecked_mut(row * self.cols + col)
|
||||||
self.data.get_unchecked_mut(row * cols + col)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Access a certain element in the grid.
|
/// Access a certain element in the grid.
|
||||||
/// Returns None if an element beyond the grid bounds is tried to be accessed.
|
/// Returns None if an element beyond the grid bounds is tried to be accessed.
|
||||||
|
#[must_use]
|
||||||
pub fn get(&self, row: usize, col: usize) -> Option<&T> {
|
pub fn get(&self, row: usize, col: usize) -> Option<&T> {
|
||||||
if row < self.rows && col < self.cols {
|
if row < self.rows && col < self.cols {
|
||||||
unsafe { Some(self.get_unchecked(row, col)) }
|
unsafe { Some(self.get_unchecked(row, col)) }
|
||||||
|
@ -248,6 +250,7 @@ impl<T> Grid<T> {
|
||||||
|
|
||||||
/// Mutable access to a certain element in the grid.
|
/// Mutable access to a certain element in the grid.
|
||||||
/// Returns None if an element beyond the grid bounds is tried to be accessed.
|
/// Returns None if an element beyond the grid bounds is tried to be accessed.
|
||||||
|
#[must_use]
|
||||||
pub fn get_mut(&mut self, row: usize, col: usize) -> Option<&mut T> {
|
pub fn get_mut(&mut self, row: usize, col: usize) -> Option<&mut T> {
|
||||||
if row < self.rows && col < self.cols {
|
if row < self.rows && col < self.cols {
|
||||||
unsafe { Some(self.get_unchecked_mut(row, col)) }
|
unsafe { Some(self.get_unchecked_mut(row, col)) }
|
||||||
|
@ -280,7 +283,7 @@ impl<T> Grid<T> {
|
||||||
/// assert!(grid.is_empty());
|
/// assert!(grid.is_empty());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.cols == 0 && self.rows == 0
|
return self.data.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clears the grid.
|
/// Clears the grid.
|
||||||
|
|
Loading…
Reference in a new issue