blob: a1e7b3cf1a78356af382059182e8434e5fbfd22f [file] [log] [blame]
Yiming Jingcf21fc42021-07-16 13:23:26 -07001#![feature(test)]
2
3extern crate test;
4
5use num_bigint::BigUint;
6use num_traits::One;
7use std::ops::{Div, Mul};
8use test::Bencher;
9
10#[bench]
11fn factorial_mul_biguint(b: &mut Bencher) {
12 b.iter(|| {
13 (1u32..1000)
14 .map(BigUint::from)
15 .fold(BigUint::one(), Mul::mul)
16 });
17}
18
19#[bench]
20fn factorial_mul_u32(b: &mut Bencher) {
21 b.iter(|| (1u32..1000).fold(BigUint::one(), Mul::mul));
22}
23
24// The division test is inspired by this blog comparison:
25// <https://tiehuis.github.io/big-integers-in-zig#division-test-single-limb>
26
27#[bench]
28fn factorial_div_biguint(b: &mut Bencher) {
29 let n: BigUint = (1u32..1000).fold(BigUint::one(), Mul::mul);
30 b.iter(|| {
31 (1u32..1000)
32 .rev()
33 .map(BigUint::from)
34 .fold(n.clone(), Div::div)
35 });
36}
37
38#[bench]
39fn factorial_div_u32(b: &mut Bencher) {
40 let n: BigUint = (1u32..1000).fold(BigUint::one(), Mul::mul);
41 b.iter(|| (1u32..1000).rev().fold(n.clone(), Div::div));
42}