Skip to content

Commit

Permalink
Update benchmarks to use criterion
Browse files Browse the repository at this point in the history
  • Loading branch information
urschrei committed Mar 17, 2023
1 parent 4cc41ab commit d645a2d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ documentation = "https://urschrei.github.io/lonlat_bng/"
readme = "README.md"
edition = "2018"

[dev-dependencies]
criterion = "0.4.0"

[build-dependencies]
cbindgen = "0.24.2"

Expand All @@ -35,3 +38,8 @@ codegen-units = 1
[profile.bench]
lto = true
codegen-units = 1

[[bench]]
name = "benchmarks"
harness = false

43 changes: 28 additions & 15 deletions benches/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
#![feature(test)]

extern crate test;
use test::Bencher;
#[macro_use]
extern crate criterion;
use criterion::Criterion;

use lonlat_bng;
use rand::distributions::{IndependentSample, Range};
use rand::distributions::Distribution;
use rand::distributions::Uniform;
use rand::thread_rng;

#[bench]
fn bench_threads(b: &mut Bencher) {
let num_coords = 100000;
let between_lon = Range::new(-6.379880, 1.768960);
let between_lat = Range::new(49.871159, 55.811741);
let mut rng = rand::thread_rng();
let mut lon_vec = vec![between_lon.ind_sample(&mut rng); num_coords];
let mut lat_vec = vec![between_lat.ind_sample(&mut rng); num_coords];
b.iter(|| {
lonlat_bng::convert_to_bng_threaded_vec(&mut lon_vec, &mut lat_vec);
#[allow(unused_must_use)]
fn bench_encode(c: &mut Criterion) {
let mut rng = thread_rng();
// These coordinates cover London, approximately
let between_lon = Uniform::from(-6.379880..1.768960);
let between_lat = Uniform::from(49.871159..55.811741);
let mut lon_vec: Vec<f64> = vec![];
let mut lat_vec: Vec<f64> = vec![];
(0..100000).for_each(|_| {
lon_vec.push(between_lon.sample(&mut rng));
lat_vec.push(between_lat.sample(&mut rng));
});
c.bench_function("bench encode: 100000 coordinates", move |b| {
b.iter(|| {
lonlat_bng::convert_to_bng_threaded_vec(&mut lon_vec, &mut lat_vec);
})
});
}

criterion_group!(
benches,
bench_encode,
);
criterion_main!(benches);

0 comments on commit d645a2d

Please sign in to comment.