Add multiplication/division benchmarks
Add benchmarks that test multiplies/divides of different sizes
This commit is contained in:
parent
dbd22cfdd8
commit
a1e57a48b2
|
@ -2,10 +2,33 @@
|
||||||
|
|
||||||
extern crate test;
|
extern crate test;
|
||||||
extern crate num;
|
extern crate num;
|
||||||
|
extern crate rand;
|
||||||
|
|
||||||
use std::mem::replace;
|
use std::mem::replace;
|
||||||
use test::Bencher;
|
use test::Bencher;
|
||||||
use num::{BigUint, Zero, One, FromPrimitive};
|
use num::{BigUint, Zero, One, FromPrimitive};
|
||||||
|
use num::bigint::RandBigInt;
|
||||||
|
use rand::{SeedableRng, StdRng};
|
||||||
|
|
||||||
|
fn multiply_bench(b: &mut Bencher, xbits: usize, ybits: usize) {
|
||||||
|
let seed: &[_] = &[1, 2, 3, 4];
|
||||||
|
let mut rng: StdRng = SeedableRng::from_seed(seed);
|
||||||
|
|
||||||
|
let x = rng.gen_bigint(xbits);
|
||||||
|
let y = rng.gen_bigint(ybits);
|
||||||
|
|
||||||
|
b.iter(|| &x * &y);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn divide_bench(b: &mut Bencher, xbits: usize, ybits: usize) {
|
||||||
|
let seed: &[_] = &[1, 2, 3, 4];
|
||||||
|
let mut rng: StdRng = SeedableRng::from_seed(seed);
|
||||||
|
|
||||||
|
let x = rng.gen_bigint(xbits);
|
||||||
|
let y = rng.gen_bigint(ybits);
|
||||||
|
|
||||||
|
b.iter(|| &x / &y);
|
||||||
|
}
|
||||||
|
|
||||||
fn factorial(n: usize) -> BigUint {
|
fn factorial(n: usize) -> BigUint {
|
||||||
let mut f: BigUint = One::one();
|
let mut f: BigUint = One::one();
|
||||||
|
@ -26,6 +49,36 @@ fn fib(n: usize) -> BigUint {
|
||||||
f0
|
f0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn multiply_0(b: &mut Bencher) {
|
||||||
|
multiply_bench(b, 1 << 8, 1 << 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn multiply_1(b: &mut Bencher) {
|
||||||
|
multiply_bench(b, 1 << 8, 1 << 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn multiply_2(b: &mut Bencher) {
|
||||||
|
multiply_bench(b, 1 << 16, 1 << 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn divide_0(b: &mut Bencher) {
|
||||||
|
divide_bench(b, 1 << 8, 1 << 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn divide_1(b: &mut Bencher) {
|
||||||
|
divide_bench(b, 1 << 12, 1 << 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn divide_2(b: &mut Bencher) {
|
||||||
|
divide_bench(b, 1 << 16, 1 << 12);
|
||||||
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn factorial_100(b: &mut Bencher) {
|
fn factorial_100(b: &mut Bencher) {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
|
|
Loading…
Reference in New Issue