|
| 1 | +// This file is part of the uutils coreutils package. |
| 2 | +// |
| 3 | +// For the full copyright and license information, please view the LICENSE |
| 4 | +// file that was distributed with this source code. |
| 5 | + |
| 6 | +use divan::{Bencher, black_box}; |
| 7 | +use uu_factor::uumain; |
| 8 | +use uucore::benchmark::run_util_function; |
| 9 | + |
| 10 | +/// Benchmark multiple u64 digits |
| 11 | +#[divan::bench(args = [(2)])] |
| 12 | +fn factor_multiple_u64s(bencher: Bencher, start_num: u64) { |
| 13 | + bencher |
| 14 | + // this is a range of 5000 different u128 integers |
| 15 | + .with_inputs(|| (start_num, start_num + 2500)) |
| 16 | + .bench_values(|(start_u64, end_u64)| { |
| 17 | + for u64_digit in start_u64..=end_u64 { |
| 18 | + black_box(run_util_function(uumain, &[&u64_digit.to_string()])); |
| 19 | + } |
| 20 | + }); |
| 21 | +} |
| 22 | + |
| 23 | +/// Benchmark multiple u128 digits |
| 24 | +#[divan::bench(args = [(18446744073709551616)])] |
| 25 | +fn factor_multiple_u128s(bencher: Bencher, start_num: u128) { |
| 26 | + bencher |
| 27 | + .with_inputs(|| { |
| 28 | + // this is a range of 1000 different u128 integers |
| 29 | + (start_num, start_num + 1000) |
| 30 | + }) |
| 31 | + .bench_values(|(start_u128, end_u128)| { |
| 32 | + for u128_digit in start_u128..=end_u128 { |
| 33 | + black_box(run_util_function(uumain, &[&u128_digit.to_string()])); |
| 34 | + } |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +/// Benchmark multiple > u128::MAX digits |
| 39 | +#[divan::bench] |
| 40 | +fn factor_multiple_big_uint(bencher: Bencher) { |
| 41 | + // max u128 value is 340_282_366_920_938_463_463_374_607_431_768_211_455 |
| 42 | + bencher |
| 43 | + // this is a range of 3 different BigUints. The range is small due to |
| 44 | + // some BigUints being unable to be factorized into prime numbers properly |
| 45 | + .with_inputs(|| (768_211_459_u64, 768_211_461_u64)) |
| 46 | + .bench_values(|(start_big_uint, end_big_uint)| { |
| 47 | + for digit in start_big_uint..=end_big_uint { |
| 48 | + let big_uint_str = format!("340282366920938463463374607431768211456{digit}"); |
| 49 | + black_box(run_util_function(uumain, &[&big_uint_str])); |
| 50 | + } |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +fn main() { |
| 55 | + divan::main(); |
| 56 | +} |
0 commit comments