microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
jan

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/paulimer/benches/bitmatrix_benchmark.rs

49lines · modecode

1use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
2use paulimer::bits::BitMatrix;
3use rand::prelude::*;
4
5struct Parameters((f64, usize));
6
7pub 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(&parameters),
15 &parameters,
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
30impl 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
37criterion_group!(benches, echelon_form_benchmark);
38criterion_main!(benches);
39
40fn 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}