From 57099e2a0eff2594ac7807263e5ec7a34ec04df6 Mon Sep 17 00:00:00 2001 From: Armin Becher Date: Wed, 1 Apr 2020 00:26:00 +0200 Subject: [PATCH] Add first benchmark --- Cargo.toml | 14 ++++++++++++++ benches/two_dim_array.rs | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 Cargo.toml create mode 100644 benches/two_dim_array.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5f5a91d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "nvector" +version = "0.1.0" +authors = ["Armin Becher "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +criterion = "0.3.1" + +[[bench]] +name = "two_dim_array" +harness = false \ No newline at end of file diff --git a/benches/two_dim_array.rs b/benches/two_dim_array.rs new file mode 100644 index 0000000..7cb4c3d --- /dev/null +++ b/benches/two_dim_array.rs @@ -0,0 +1,20 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; + +fn init(x: usize, y: usize) -> Vec> { + vec![vec![42; x]; y] +} + +fn init_flat(x: usize, y: usize) -> Vec { + vec![42; x * y] +} + + +fn criterion_benchmark(c: &mut Criterion) { + c.bench_function("Init 20x20", |b| b.iter(|| init(black_box(20), black_box(20)))); + c.bench_function("Init flat 20x20", |b| b.iter(|| init_flat(black_box(20), black_box(20)))); + c.bench_function("Init 2000x2000", |b| b.iter(|| init(black_box(2000), black_box(2000)))); + c.bench_function("Init flat 2000x2000", |b| b.iter(|| init_flat(black_box(2000), black_box(2000)))); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches);