From 4167ba82deefedc202bb9178dc147dc5a5489602 Mon Sep 17 00:00:00 2001 From: Ulrich Hornung Date: Sat, 23 Mar 2024 21:05:36 +0100 Subject: [PATCH] adapt performance benchmark. reduce to factor function --- tests/benches/factor/Cargo.toml | 6 ++--- tests/benches/factor/benches/gcd.rs | 33 --------------------------- tests/benches/factor/benches/table.rs | 21 +++++++---------- 3 files changed, 11 insertions(+), 49 deletions(-) delete mode 100644 tests/benches/factor/benches/gcd.rs diff --git a/tests/benches/factor/Cargo.toml b/tests/benches/factor/Cargo.toml index 26900e78a91..5ffcd66fb4c 100644 --- a/tests/benches/factor/Cargo.toml +++ b/tests/benches/factor/Cargo.toml @@ -17,10 +17,10 @@ array-init = "2.0.0" criterion = "0.3" rand = "0.8" rand_chacha = "0.3.1" +num-bigint = "0.4.4" +num-prime = "0.4.3" +num-traits = "0.2.18" -[[bench]] -name = "gcd" -harness = false [[bench]] name = "table" diff --git a/tests/benches/factor/benches/gcd.rs b/tests/benches/factor/benches/gcd.rs deleted file mode 100644 index 61545145f98..00000000000 --- a/tests/benches/factor/benches/gcd.rs +++ /dev/null @@ -1,33 +0,0 @@ -// This file is part of the uutils coreutils package. -// -// For the full copyright and license information, please view the LICENSE -// file that was distributed with this source code. -use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; -use uu_factor::numeric; - -fn gcd(c: &mut Criterion) { - let inputs = { - // Deterministic RNG; use an explicitly-named RNG to guarantee stability - use rand::{RngCore, SeedableRng}; - use rand_chacha::ChaCha8Rng; - const SEED: u64 = 0xab4d_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); diff --git a/tests/benches/factor/benches/table.rs b/tests/benches/factor/benches/table.rs index f666d72d510..90a61c0fb10 100644 --- a/tests/benches/factor/benches/table.rs +++ b/tests/benches/factor/benches/table.rs @@ -4,7 +4,10 @@ // file that was distributed with this source code. use array_init::array_init; use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; -use uu_factor::{table::*, Factors}; +use std::{collections::BTreeMap, mem}; +use num_bigint::BigUint; +use num_traits::FromPrimitive; + fn table(c: &mut Criterion) { #[cfg(target_os = "linux")] @@ -26,21 +29,13 @@ fn table(c: &mut Criterion) { group.throughput(Throughput::Elements(INPUT_SIZE as _)); for a in inputs.take(10) { let a_str = format!("{:?}", a); - group.bench_with_input(BenchmarkId::new("factor_chunk", &a_str), &a, |b, &a| { - b.iter(|| { - let mut n_s = a; - let mut f_s: [_; INPUT_SIZE] = array_init(|_| Factors::one()); - for (n_s, f_s) in n_s.chunks_mut(CHUNK_SIZE).zip(f_s.chunks_mut(CHUNK_SIZE)) { - factor_chunk(n_s.try_into().unwrap(), f_s.try_into().unwrap()); - } - }); - }); group.bench_with_input(BenchmarkId::new("factor", &a_str), &a, |b, &a| { b.iter(|| { let mut n_s = a; - let mut f_s: [_; INPUT_SIZE] = array_init(|_| Factors::one()); - for (n, f) in n_s.iter_mut().zip(f_s.iter_mut()) { - factor(n, f); + let mut f_s: [BTreeMap; INPUT_SIZE] = array_init(|_| BTreeMap::new()); + for (&mut n, f) in n_s.iter_mut().zip(f_s.iter_mut()) { + let r: BTreeMap = num_prime::nt_funcs::factorize(BigUint::from_u64(n).unwrap()); + _ = mem::replace(f, r); } }); });