1 use criterion::{criterion_group, criterion_main, Criterion};
2 use grid::grid;
3 use grid::Grid;
4 use rand::Rng;
5
6 const SIZE: usize = 1_000;
7
init_vec_vec() -> Vec<Vec<u8>>8 fn init_vec_vec() -> Vec<Vec<u8>> {
9 vec![vec![0; SIZE]; SIZE]
10 }
11
init_grid() -> Grid<u8>12 fn init_grid() -> Grid<u8> {
13 Grid::init(SIZE, SIZE, 0)
14 }
15
criterion_benchmark(c: &mut Criterion)16 fn criterion_benchmark(c: &mut Criterion) {
17 let mut rng = rand::rng();
18 let mut rand = || rng.random_range(0..SIZE);
19
20 // Init macro
21 c.bench_function("vecvec_init_macro", |b| {
22 b.iter(|| {
23 vec![
24 vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
25 vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
26 vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
27 vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
28 vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
29 vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
30 vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
31 vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
32 vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
33 vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
34 ]
35 })
36 });
37 c.bench_function("grid_init_macro", |b| {
38 b.iter(|| {
39 grid![[0,1,2,3,4,5,6,7,8,9]
40 [0,1,2,3,4,5,6,7,8,9]
41 [0,1,2,3,4,5,6,7,8,9]
42 [0,1,2,3,4,5,6,7,8,9]
43 [0,1,2,3,4,5,6,7,8,9]
44 [0,1,2,3,4,5,6,7,8,9]
45 [0,1,2,3,4,5,6,7,8,9]
46 [0,1,2,3,4,5,6,7,8,9]
47 [0,1,2,3,4,5,6,7,8,9]
48 [0,1,2,3,4,5,6,7,8,9]]
49 })
50 });
51 c.bench_function("grid_from_vec", |b| {
52 b.iter(|| {
53 let vec = vec![
54 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,
55 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,
56 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,
57 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
58 ];
59 Grid::from_vec(vec, 10)
60 })
61 });
62
63 // Constructor
64 c.bench_function("vecvec_init", |b| b.iter(|| vec![vec![0; SIZE]; SIZE]));
65 c.bench_function("grid_init", |b| b.iter(|| Grid::init(SIZE, SIZE, 0)));
66
67 // Index
68 c.bench_function("vecvec_idx", |b| {
69 let vec_vec = init_vec_vec();
70 b.iter_batched(
71 || (rand(), rand()),
72 |(x, y)| {
73 let _v = vec_vec[x][y];
74 },
75 criterion::BatchSize::SmallInput,
76 )
77 });
78 c.bench_function("grid_idx", |b| {
79 let grid = init_grid();
80 b.iter_batched(
81 || (rand(), rand()),
82 |(x, y)| grid[(x, y)],
83 criterion::BatchSize::SmallInput,
84 )
85 });
86 c.bench_function("vecvec_get", |b| {
87 let vec_vec = init_vec_vec();
88 b.iter_batched(
89 || (rand(), rand()),
90 |(x, y)| {
91 let _v = vec_vec.get(x).unwrap().get(y).unwrap();
92 },
93 criterion::BatchSize::SmallInput,
94 )
95 });
96 c.bench_function("grid_get", |b| {
97 let grid = init_grid();
98 b.iter_batched(
99 || (rand(), rand()),
100 |(x, y)| {
101 let _v = grid.get(x, y).unwrap();
102 },
103 criterion::BatchSize::SmallInput,
104 )
105 });
106
107 //Set
108 c.bench_function("vecvec_set", |b| {
109 let mut vec_vec = init_vec_vec();
110 b.iter_batched(
111 || (rand(), rand()),
112 |(x, y)| vec_vec[x][y] = 42,
113 criterion::BatchSize::SmallInput,
114 )
115 });
116 c.bench_function("grid_set", |b| {
117 let mut g = init_grid();
118 b.iter_batched(
119 || (rand(), rand()),
120 |(x, y)| g[(x, y)] = 42,
121 criterion::BatchSize::SmallInput,
122 )
123 });
124
125 // Push
126 c.bench_function("grid_push_row", |b| {
127 let grid = init_grid();
128 b.iter_batched(
129 || grid.clone(),
130 |mut g| g.push_row(vec![0; SIZE]),
131 criterion::BatchSize::SmallInput,
132 )
133 });
134 c.bench_function("grid_push_col", |b| {
135 let grid = init_grid();
136 b.iter_batched(
137 || grid.clone(),
138 |mut g| g.push_col(vec![0; SIZE]),
139 criterion::BatchSize::SmallInput,
140 )
141 });
142
143 // Pop
144 c.bench_function("grid_pop_row", |b| {
145 let grid = init_grid();
146 b.iter_batched(
147 || grid.clone(),
148 |mut g| g.pop_row(),
149 criterion::BatchSize::SmallInput,
150 )
151 });
152 c.bench_function("grid_pop_col", |b| {
153 let grid = init_grid();
154 b.iter_batched(
155 || grid.clone(),
156 |mut g| g.pop_col(),
157 criterion::BatchSize::SmallInput,
158 )
159 });
160
161 // Remove
162 c.bench_function("grid_remove_row", |b| {
163 let grid = init_grid();
164 b.iter_batched(
165 || grid.clone(),
166 |mut g| g.remove_row(2),
167 criterion::BatchSize::SmallInput,
168 )
169 });
170 c.bench_function("grid_remove_col", |b| {
171 let grid = init_grid();
172 b.iter_batched(
173 || grid.clone(),
174 |mut g| g.remove_col(2),
175 criterion::BatchSize::SmallInput,
176 )
177 });
178
179 // Rotation
180 c.bench_function("grid_rotate_left", |b| {
181 let grid = init_grid();
182 b.iter_batched(
183 || grid.clone(),
184 |mut g| g.rotate_left(),
185 criterion::BatchSize::SmallInput,
186 )
187 });
188 c.bench_function("grid_rotate_right", |b| {
189 let grid = init_grid();
190 b.iter_batched(
191 || grid.clone(),
192 |mut g| g.rotate_right(),
193 criterion::BatchSize::SmallInput,
194 )
195 });
196 c.bench_function("grid_rotate_half", |b| {
197 let grid = init_grid();
198 b.iter_batched(
199 || grid.clone(),
200 |mut g| g.rotate_half(),
201 criterion::BatchSize::SmallInput,
202 )
203 });
204 }
205
206 criterion_group!(benches, criterion_benchmark);
207 criterion_main!(benches);
208