From 4d6b38264df8c82be91f14bb161538d3c847c004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20S=C3=B6rng=C3=A5rd?= Date: Tue, 30 Jul 2024 11:23:11 +0200 Subject: [PATCH] Add a random benchmark --- Cargo.lock | 84 ++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + benches/lambert_benches.rs | 55 ++++++++++++++++++++++--- 3 files changed, 134 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fbb72a2..a664f92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,6 +44,12 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "cast" version = "0.3.0" @@ -181,6 +187,17 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "half" version = "2.4.1" @@ -238,6 +255,7 @@ version = "0.3.1" dependencies = [ "approx", "criterion", + "rand", ] [[package]] @@ -307,6 +325,15 @@ dependencies = [ "plotters-backend", ] +[[package]] +name = "ppv-lite86" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dee4364d9f3b902ef14fab8a1ddffb783a1cb6b4bba3bfc1fa3922732c7de97f" +dependencies = [ + "zerocopy", +] + [[package]] name = "proc-macro2" version = "1.0.86" @@ -325,6 +352,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "rayon" version = "1.10.0" @@ -457,6 +514,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasm-bindgen" version = "0.2.92" @@ -602,3 +665,24 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "zerocopy" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "854e949ac82d619ee9a14c66a1b674ac730422372ccb759ce0c39cabcf2bf8e6" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "125139de3f6b9d625c39e2efdd73d41bdac468ccd556556440e322be0e1bbd91" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index 5dcb858..e3478a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/benches/lambert_benches.rs b/benches/lambert_benches.rs index 9b0218c..ff9af90 100644 --- a/benches/lambert_benches.rs +++ b/benches/lambert_benches.rs @@ -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 = [ @@ -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 = (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 = (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 = (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 = (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| { @@ -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)))); } }