Skip to content

Commit

Permalink
adapt performance benchmark. reduce to factor function
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture committed Mar 23, 2024
1 parent 60b5940 commit 4167ba8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 49 deletions.
6 changes: 3 additions & 3 deletions tests/benches/factor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
33 changes: 0 additions & 33 deletions tests/benches/factor/benches/gcd.rs

This file was deleted.

21 changes: 8 additions & 13 deletions tests/benches/factor/benches/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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<BigUint, usize>; INPUT_SIZE] = array_init(|_| BTreeMap::new());
for (&mut n, f) in n_s.iter_mut().zip(f_s.iter_mut()) {
let r: BTreeMap<BigUint, usize> = num_prime::nt_funcs::factorize(BigUint::from_u64(n).unwrap());
_ = mem::replace(f, r);
}
});
});
Expand Down

0 comments on commit 4167ba8

Please sign in to comment.