Skip to content

Commit

Permalink
Add a random benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
JSorngard committed Jul 30, 2024
1 parent 3f63d2d commit 4d6b382
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 6 deletions.
84 changes: 84 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ repository = "https://github.com/JSorngard/lambert_w"
[dev-dependencies]
approx = "0.5.1"
criterion = { version = "0.5.1", features = ["html_reports"]}
rand = "0.8.5"

[features]
default = ["24bits", "50bits"]
Expand Down
55 changes: 49 additions & 6 deletions benches/lambert_benches.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use criterion::{criterion_group, criterion_main, Criterion};
use lambert_w::{lambert_w_0, lambert_w_m1, sp_lambert_w_0, sp_lambert_w_m1};
use rand::{thread_rng, Rng};
use std::hint::black_box;
use std::time::Instant;

fn bench(c: &mut Criterion) {
let big_args = [
Expand All @@ -24,6 +26,51 @@ fn bench(c: &mut Criterion) {
-1.000000000000008e-145,
];

{
let mut group = c.benchmark_group("random inputs");
let mut rng = thread_rng();
group.bench_function("W_0 50 bits", |b| {
b.iter_custom(|iters| {
let datas: Vec<f64> = (0..iters).map(|_| rng.gen()).collect();
let start = Instant::now();
for &z in &datas {
black_box(lambert_w_0(z));
}
start.elapsed()
})
});
group.bench_function("W_0 24 bits", |b| {
b.iter_custom(|iters| {
let datas: Vec<f64> = (0..iters).map(|_| rng.gen()).collect();
let start = Instant::now();
for &z in &datas {
black_box(sp_lambert_w_0(z));
}
start.elapsed()
})
});
group.bench_function("W_-1 50 bits", |b| {
b.iter_custom(|iters| {
let datas: Vec<f64> = (0..iters).map(|_| rng.gen()).collect();
let start = Instant::now();
for &z in &datas {
black_box(lambert_w_m1(z));
}
start.elapsed()
})
});
group.bench_function("W_-1 24 bits", |b| {
b.iter_custom(|iters| {
let datas: Vec<f64> = (0..iters).map(|_| rng.gen()).collect();
let start = Instant::now();
for &z in &datas {
black_box(sp_lambert_w_m1(z));
}
start.elapsed()
})
});
}

for z in big_args {
let mut group = c.benchmark_group(format!("W_0({z})"));
group.bench_function(&format!("50 bits"), |b| {
Expand All @@ -35,12 +82,8 @@ fn bench(c: &mut Criterion) {
}
for z in small_args {
let mut group = c.benchmark_group(format!("W_-1({z})"));
group.bench_function(&format!("50 bits"), |b| {
b.iter(|| black_box(lambert_w_m1(z)))
});
group.bench_function(&format!("24 bits"), |b| {
b.iter(|| black_box(sp_lambert_w_m1(z)))
});
group.bench_function("50 bits", |b| b.iter(|| black_box(lambert_w_m1(z))));
group.bench_function("24 bits", |b| b.iter(|| black_box(sp_lambert_w_m1(z))));
}
}

Expand Down

0 comments on commit 4d6b382

Please sign in to comment.