|
| 1 | +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; |
| 2 | + |
| 3 | +// to run this bench you have to change the declaraion in mod.rs ---> pub mod fastpair; |
| 4 | +use smartcore::algorithm::neighbour::fastpair::FastPair; |
| 5 | +use smartcore::linalg::naive::dense_matrix::*; |
| 6 | +use std::time::Duration; |
| 7 | + |
| 8 | +fn closest_pair_bench(n: usize, m: usize) -> () { |
| 9 | + let x = DenseMatrix::<f64>::rand(n, m); |
| 10 | + let fastpair = FastPair::new(&x); |
| 11 | + let result = fastpair.unwrap(); |
| 12 | + |
| 13 | + result.closest_pair(); |
| 14 | +} |
| 15 | + |
| 16 | +fn closest_pair_brute_bench(n: usize, m: usize) -> () { |
| 17 | + let x = DenseMatrix::<f64>::rand(n, m); |
| 18 | + let fastpair = FastPair::new(&x); |
| 19 | + let result = fastpair.unwrap(); |
| 20 | + |
| 21 | + result.closest_pair_brute(); |
| 22 | +} |
| 23 | + |
| 24 | +fn bench_fastpair(c: &mut Criterion) { |
| 25 | + let mut group = c.benchmark_group("FastPair"); |
| 26 | + |
| 27 | + // with full samples size (100) the test will take too long |
| 28 | + group.significance_level(0.1).sample_size(30); |
| 29 | + // increase from default 5.0 secs |
| 30 | + group.measurement_time(Duration::from_secs(60)); |
| 31 | + |
| 32 | + for n_samples in [100_usize, 1000_usize].iter() { |
| 33 | + for n_features in [10_usize, 100_usize, 1000_usize].iter() { |
| 34 | + group.bench_with_input( |
| 35 | + BenchmarkId::from_parameter(format!( |
| 36 | + "fastpair --- n_samples: {}, n_features: {}", |
| 37 | + n_samples, n_features |
| 38 | + )), |
| 39 | + n_samples, |
| 40 | + |b, _| b.iter(|| closest_pair_bench(*n_samples, *n_features)), |
| 41 | + ); |
| 42 | + group.bench_with_input( |
| 43 | + BenchmarkId::from_parameter(format!( |
| 44 | + "brute --- n_samples: {}, n_features: {}", |
| 45 | + n_samples, n_features |
| 46 | + )), |
| 47 | + n_samples, |
| 48 | + |b, _| b.iter(|| closest_pair_brute_bench(*n_samples, *n_features)), |
| 49 | + ); |
| 50 | + } |
| 51 | + } |
| 52 | + group.finish(); |
| 53 | +} |
| 54 | + |
| 55 | +criterion_group!(benches, bench_fastpair); |
| 56 | +criterion_main!(benches); |
0 commit comments