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