1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
#![feature(test)]
#![cfg(feature = "rand")]
extern crate test;
use num_bigint::{BigUint, RandBigInt};
use test::Bencher;
mod rng;
use rng::get_rng;
// The `big64` cases demonstrate the speed of cases where the value
// can be converted to a `u64` primitive for faster calculation.
//
// The `big1k` cases demonstrate those that can convert to `f64` for
// a better initial guess of the actual value.
//
// The `big2k` and `big4k` cases are too big for `f64`, and use a simpler guess.
fn check(x: &BigUint, n: u32) {
let root = x.nth_root(n);
if n == 2 {
assert_eq!(root, x.sqrt())
} else if n == 3 {
assert_eq!(root, x.cbrt())
}
let lo = root.pow(n);
assert!(lo <= *x);
assert_eq!(lo.nth_root(n), root);
assert_eq!((&lo - 1u32).nth_root(n), &root - 1u32);
let hi = (&root + 1u32).pow(n);
assert!(hi > *x);
assert_eq!(hi.nth_root(n), &root + 1u32);
assert_eq!((&hi - 1u32).nth_root(n), root);
}
fn bench_sqrt(b: &mut Bencher, bits: u64) {
let x = get_rng().gen_biguint(bits);
eprintln!("bench_sqrt({})", x);
check(&x, 2);
b.iter(|| x.sqrt());
}
#[bench]
fn big64_sqrt(b: &mut Bencher) {
bench_sqrt(b, 64);
}
#[bench]
fn big1k_sqrt(b: &mut Bencher) {
bench_sqrt(b, 1024);
}
#[bench]
fn big2k_sqrt(b: &mut Bencher) {
bench_sqrt(b, 2048);
}
#[bench]
fn big4k_sqrt(b: &mut Bencher) {
bench_sqrt(b, 4096);
}
fn bench_cbrt(b: &mut Bencher, bits: u64) {
let x = get_rng().gen_biguint(bits);
eprintln!("bench_cbrt({})", x);
check(&x, 3);
b.iter(|| x.cbrt());
}
#[bench]
fn big64_cbrt(b: &mut Bencher) {
bench_cbrt(b, 64);
}
#[bench]
fn big1k_cbrt(b: &mut Bencher) {
bench_cbrt(b, 1024);
}
#[bench]
fn big2k_cbrt(b: &mut Bencher) {
bench_cbrt(b, 2048);
}
#[bench]
fn big4k_cbrt(b: &mut Bencher) {
bench_cbrt(b, 4096);
}
fn bench_nth_root(b: &mut Bencher, bits: u64, n: u32) {
let x = get_rng().gen_biguint(bits);
eprintln!("bench_{}th_root({})", n, x);
check(&x, n);
b.iter(|| x.nth_root(n));
}
#[bench]
fn big64_nth_10(b: &mut Bencher) {
bench_nth_root(b, 64, 10);
}
#[bench]
fn big1k_nth_10(b: &mut Bencher) {
bench_nth_root(b, 1024, 10);
}
#[bench]
fn big1k_nth_100(b: &mut Bencher) {
bench_nth_root(b, 1024, 100);
}
#[bench]
fn big1k_nth_1000(b: &mut Bencher) {
bench_nth_root(b, 1024, 1000);
}
#[bench]
fn big1k_nth_10000(b: &mut Bencher) {
bench_nth_root(b, 1024, 10000);
}
#[bench]
fn big2k_nth_10(b: &mut Bencher) {
bench_nth_root(b, 2048, 10);
}
#[bench]
fn big2k_nth_100(b: &mut Bencher) {
bench_nth_root(b, 2048, 100);
}
#[bench]
fn big2k_nth_1000(b: &mut Bencher) {
bench_nth_root(b, 2048, 1000);
}
#[bench]
fn big2k_nth_10000(b: &mut Bencher) {
bench_nth_root(b, 2048, 10000);
}
#[bench]
fn big4k_nth_10(b: &mut Bencher) {
bench_nth_root(b, 4096, 10);
}
#[bench]
fn big4k_nth_100(b: &mut Bencher) {
bench_nth_root(b, 4096, 100);
}
#[bench]
fn big4k_nth_1000(b: &mut Bencher) {
bench_nth_root(b, 4096, 1000);
}
#[bench]
fn big4k_nth_10000(b: &mut Bencher) {
bench_nth_root(b, 4096, 10000);
}
|