microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/paulimer/benches/bitmatrix_benchmark.rs
49lines · modecode
| 1 | use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion}; |
| 2 | use paulimer::bits::BitMatrix; |
| 3 | use rand::prelude::*; |
| 4 | |
| 5 | struct Parameters((f64, usize)); |
| 6 | |
| 7 | pub fn echelon_form_benchmark(criterion: &mut Criterion) { |
| 8 | let mut group = criterion.benchmark_group("Bitmatrix::echelon_form"); |
| 9 | for sparsity in [0.5, 0.1, 0.01, 0.001] { |
| 10 | for size in [100usize, 1000usize, 10000usize] { |
| 11 | group.sample_size(10); |
| 12 | let parameters = Parameters((sparsity, size)); |
| 13 | group.bench_with_input( |
| 14 | BenchmarkId::from_parameter(¶meters), |
| 15 | ¶meters, |
| 16 | |bencher, parameters| { |
| 17 | let (sparsity, size) = parameters.0; |
| 18 | bencher.iter_batched( |
| 19 | || random_bitmatrix(size, size, sparsity), |
| 20 | |mut matrix| matrix.echelonize(), |
| 21 | BatchSize::SmallInput, |
| 22 | ); |
| 23 | }, |
| 24 | ); |
| 25 | } |
| 26 | } |
| 27 | group.finish(); |
| 28 | } |
| 29 | |
| 30 | impl std::fmt::Display for Parameters { |
| 31 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 32 | write!(f, "{:?}", self.0)?; |
| 33 | Ok(()) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | criterion_group!(benches, echelon_form_benchmark); |
| 38 | criterion_main!(benches); |
| 39 | |
| 40 | fn random_bitmatrix(rowcount: usize, columncount: usize, sparsity: f64) -> BitMatrix { |
| 41 | let mut matrix = BitMatrix::with_shape(rowcount, columncount); |
| 42 | let mut bits = std::iter::from_fn(move || Some(thread_rng().gen_bool(sparsity))); |
| 43 | for row_index in 0..rowcount { |
| 44 | for column_index in 0..columncount { |
| 45 | matrix.set((row_index, column_index), bits.next().expect("boom")); |
| 46 | } |
| 47 | } |
| 48 | matrix |
| 49 | } |