Speedup init vec macro
This commit is contained in:
parent
b7dff2e573
commit
00a5677ff6
2 changed files with 19 additions and 4 deletions
|
@ -51,6 +51,17 @@ fn criterion_benchmark(c: &mut Criterion) {
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
c.bench_function("Macro init grid from_vec", |b| {
|
||||||
|
b.iter(|| {
|
||||||
|
let vec = 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,
|
||||||
|
];
|
||||||
|
Grid::from_vec(vec, 10)
|
||||||
|
})
|
||||||
|
});
|
||||||
c.bench_function("Macro init grid", |b| {
|
c.bench_function("Macro init grid", |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]
|
||||||
|
|
12
src/lib.rs
12
src/lib.rs
|
@ -40,17 +40,21 @@ macro_rules! grid {
|
||||||
( [$( $x0:expr ),*] $([$( $x:expr ),*])* ) => {
|
( [$( $x0:expr ),*] $([$( $x:expr ),*])* ) => {
|
||||||
{
|
{
|
||||||
let mut _assert_width0 = [(); $crate::count!($($x0)*)];
|
let mut _assert_width0 = [(); $crate::count!($($x0)*)];
|
||||||
let mut vec = Vec::new();
|
|
||||||
let cols = $crate::count!($($x0)*);
|
let cols = $crate::count!($($x0)*);
|
||||||
|
let rows = 1usize;
|
||||||
$( vec.push($x0); )*
|
|
||||||
|
|
||||||
$(
|
$(
|
||||||
let _assert_width = [(); $crate::count!($($x)*)];
|
let _assert_width = [(); $crate::count!($($x)*)];
|
||||||
_assert_width0 = _assert_width;
|
_assert_width0 = _assert_width;
|
||||||
$( vec.push($x); )*
|
let rows = rows + 1usize;
|
||||||
)*
|
)*
|
||||||
|
|
||||||
|
let mut vec = Vec::with_capacity(rows * cols);
|
||||||
|
|
||||||
|
$( vec.push($x0); )*
|
||||||
|
$( $( vec.push($x); )* )*
|
||||||
|
|
||||||
$crate::Grid::from_vec(vec, cols)
|
$crate::Grid::from_vec(vec, cols)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue