|
3 | 3 | // For the full copyright and license information, please view the LICENSE |
4 | 4 | // file that was distributed with this source code. |
5 | 5 |
|
6 | | -// spell-checker:ignore funcs |
| 6 | +// spell-checker:ignore funcs semiprimes |
7 | 7 |
|
8 | 8 | use divan::{Bencher, black_box}; |
9 | 9 | use uu_factor::uumain; |
10 | 10 | use uucore::benchmark::run_util_function; |
11 | 11 |
|
12 | | -/// Benchmark multiple u64 digits |
| 12 | +/// Benchmark factoring a range of small consecutive integers |
| 13 | +/// This tests the trial division fast path for small numbers |
| 14 | +#[divan::bench(args = [1000, 5000])] |
| 15 | +fn factor_small_range(bencher: Bencher, count: usize) { |
| 16 | + let numbers: Vec<String> = (2..=(count as u64)).map(|n| n.to_string()).collect(); |
| 17 | + bencher.bench(|| { |
| 18 | + for num_str in &numbers { |
| 19 | + black_box(run_util_function(uumain, &[num_str])); |
| 20 | + } |
| 21 | + }); |
| 22 | +} |
| 23 | + |
| 24 | +/// Benchmark factoring small primes |
| 25 | +#[divan::bench(args = [ |
| 26 | + ("prime_1009", "1009"), |
| 27 | + ("prime_10007", "10007"), |
| 28 | + ("prime_100003", "100003"), |
| 29 | +])] |
| 30 | +fn factor_small_primes(bencher: Bencher, (_name, num_str): (&str, &str)) { |
| 31 | + bencher.bench(|| { |
| 32 | + black_box(run_util_function(uumain, &[num_str])); |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +/// Benchmark factoring large u64 primes |
| 37 | +/// These require primality testing but no factorization |
| 38 | +#[divan::bench(args = [ |
| 39 | + ("prime_near_u32_max", "4294967291"), |
| 40 | + ("prime_near_i64_max", "9223372036854775783"), |
| 41 | + ("prime_near_u64_max", "18446744073709551557"), |
| 42 | +])] |
| 43 | +fn factor_large_u64_primes(bencher: Bencher, (_name, num_str): (&str, &str)) { |
| 44 | + bencher.bench(|| { |
| 45 | + black_box(run_util_function(uumain, &[num_str])); |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +/// Benchmark factoring u64 semiprimes (product of two primes) |
| 50 | +/// These exercise the Pollard-Rho factorization algorithm |
| 51 | +#[divan::bench(args = [ |
| 52 | + ("semiprime_32bit", "3215031751"), |
| 53 | + ("semiprime_48bit", "281474976710597"), |
| 54 | + ("fermat_number_f5", "4294967297"), |
| 55 | +])] |
| 56 | +fn factor_u64_semiprimes(bencher: Bencher, (_name, num_str): (&str, &str)) { |
| 57 | + bencher.bench(|| { |
| 58 | + black_box(run_util_function(uumain, &[num_str])); |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +/// Benchmark factoring highly composite numbers |
| 63 | +/// These have many small factors and test trial division efficiency |
| 64 | +#[divan::bench(args = [ |
| 65 | + ("primorial_7", "510510"), |
| 66 | + ("factorial_12", "479001600"), |
| 67 | + ("highly_composite", "720720"), |
| 68 | +])] |
| 69 | +fn factor_highly_composite(bencher: Bencher, (_name, num_str): (&str, &str)) { |
| 70 | + bencher.bench(|| { |
| 71 | + black_box(run_util_function(uumain, &[num_str])); |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +/// Benchmark the maximum u64 value |
| 76 | +/// 2^64 - 1 = 3 × 5 × 17 × 257 × 641 × 65537 × 6700417 |
| 77 | +#[divan::bench] |
| 78 | +fn factor_u64_max(bencher: Bencher) { |
| 79 | + bencher.bench(|| { |
| 80 | + black_box(run_util_function(uumain, &["18446744073709551615"])); |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +/// Benchmark factoring Mersenne prime M61 (2^61 - 1) |
| 85 | +/// This is a prime number, so it tests primality checking for large u64 |
| 86 | +#[divan::bench] |
| 87 | +fn factor_mersenne_61(bencher: Bencher) { |
| 88 | + // 2^61 - 1 = 2305843009213693951 (prime) |
| 89 | + bencher.bench(|| { |
| 90 | + black_box(run_util_function(uumain, &["2305843009213693951"])); |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +/// Benchmark factoring 100-bit numbers with many small factors |
| 95 | +/// This tests the u128 factorization path with numbers that factor quickly |
| 96 | +#[divan::bench(args = [ |
| 97 | + ("100bit_smooth", "123456789012345678901234567890"), |
| 98 | +])] |
| 99 | +fn factor_100bit_numbers(bencher: Bencher, (_name, num_str): (&str, &str)) { |
| 100 | + bencher.bench(|| { |
| 101 | + black_box(run_util_function(uumain, &[num_str])); |
| 102 | + }); |
| 103 | +} |
| 104 | + |
| 105 | +/// Benchmark factoring 128-bit numbers |
| 106 | +/// Tests various 128-bit cases: prime, power of 2, and smooth numbers |
| 107 | +#[divan::bench(args = [ |
| 108 | + ("128bit_prime", "340282366920938463463374607431768211297"), |
| 109 | + ("128bit_power_of_2", "340282366920938463463374607431768211456"), |
| 110 | + ("128bit_smooth", "340282366920938463463374607431768211455"), |
| 111 | +])] |
| 112 | +fn factor_128bit_numbers(bencher: Bencher, (_name, num_str): (&str, &str)) { |
| 113 | + bencher.bench(|| { |
| 114 | + black_box(run_util_function(uumain, &[num_str])); |
| 115 | + }); |
| 116 | +} |
| 117 | + |
| 118 | +/// Benchmark processing multiple numbers in sequence |
| 119 | +/// This tests the overhead of repeated invocations |
13 | 120 | #[divan::bench(args = [(2)])] |
14 | 121 | fn factor_multiple_u64s(bencher: Bencher, start_num: u64) { |
15 | 122 | bencher |
16 | | - // this is a range of 5000 different u128 integers |
17 | 123 | .with_inputs(|| (start_num, start_num + 2500)) |
18 | 124 | .bench_values(|(start_u64, end_u64)| { |
19 | 125 | for u64_digit in start_u64..=end_u64 { |
|
0 commit comments