factor::numeric::gcd: Add criterion-powered benchmark

The function had to be made `pub`, this is a [known limitation] of Criterion.

[known limitation]: https://bheisler.github.io/criterion.rs/book/user_guide/known_limitations.html
This commit is contained in:
nicoo
2020-07-17 20:16:50 +02:00
parent 1b593d94c9
commit 4f23767b85
5 changed files with 396 additions and 2 deletions
Generated
+359
View File
File diff suppressed because it is too large Load Diff
+6
View File
@@ -22,8 +22,14 @@ uucore = { version="0.0.4", package="uucore", git="https://github.com/uutils/uuc
uucore_procs = { version="0.0.4", package="uucore_procs", git="https://github.com/uutils/uucore.git", branch="canary" }
[dev-dependencies]
criterion = "0.3"
paste = "0.1.18"
quickcheck = "0.9.2"
rand_chacha = "0.2.2"
[[bench]]
name = "gcd"
harness = false
[[bin]]
name = "factor"
+29
View File
@@ -0,0 +1,29 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use uu_factor::numeric;
fn gcd(c: &mut Criterion) {
let inputs = {
// Deterministic RNG; use an explicitely-named RNG to guarantee stability
use rand::{RngCore, SeedableRng};
use rand_chacha::ChaCha8Rng;
const SEED: u64 = 0xa_b4d_1dea_dead_cafe;
let mut rng = ChaCha8Rng::seed_from_u64(SEED);
std::iter::repeat_with(move || (rng.next_u64(), rng.next_u64()))
};
let mut group = c.benchmark_group("gcd");
for (n, m) in inputs.take(10) {
group.bench_with_input(
BenchmarkId::from_parameter(format!("{}_{}", n, m)),
&(n, m),
|b, &(n, m)| {
b.iter(|| numeric::gcd(n, m));
},
);
}
group.finish()
}
criterion_group!(benches, gcd);
criterion_main!(benches);
+1 -1
View File
@@ -16,7 +16,7 @@ mod factor;
pub(crate) use factor::*;
mod miller_rabin;
mod numeric;
pub mod numeric;
mod rho;
mod table;
+1 -1
View File
@@ -17,7 +17,7 @@ use std::mem::swap;
// This is incorrectly reported as dead code,
// presumably when included in build.rs.
#[allow(dead_code)]
pub(crate) fn gcd(mut a: u64, mut b: u64) -> u64 {
pub fn gcd(mut a: u64, mut b: u64) -> u64 {
while b > 0 {
a %= b;
swap(&mut a, &mut b);