Add factorial benchmark

This commit is contained in:
Paul Ouellette 2021-05-01 17:16:20 -04:00
parent 8227ae5c65
commit ba675a12c2
4 changed files with 67 additions and 3 deletions

View File

@ -14,3 +14,4 @@ rand = "0.8.3"
rayon = "1.5.0" rayon = "1.5.0"
hwloc2 = "2.2.0" hwloc2 = "2.2.0"
lazy_static = "1.4.0" lazy_static = "1.4.0"
num = "0.3"

View File

@ -29,8 +29,14 @@ following command:
$ cargo run --release --bin <testname> $ cargo run --release --bin <testname>
``` ```
Additional comparison programs are located in `omp/`, where C comparison C comparison benchmarks can be found in `omp/`.
benchmarks can be found.
Additional benchmarks are located in `benches`. These require Rust nightly and
can be run with the following command:
```
$ cargo +nightly bench
```
## Known issues ## Known issues

57
benches/factorial.rs Normal file
View File

@ -0,0 +1,57 @@
#![feature(test)]
extern crate test;
use num::{BigUint, One};
use rayon::prelude::*;
use rustmp::par_for;
use std::ops::Mul;
const N: u32 = 9999;
// following functions copied from rayon-demo
/// Compute the Factorial using a plain iterator.
fn factorial(n: u32) -> BigUint {
(1..=n).map(BigUint::from).fold(BigUint::one(), Mul::mul)
}
#[bench]
/// Benchmark the Factorial using a plain iterator.
fn factorial_iterator(b: &mut test::Bencher) {
let f = factorial(N);
b.iter(|| assert_eq!(factorial(test::black_box(N)), f));
}
#[bench]
/// Compute the Factorial using rayon::par_iter.
fn factorial_par_iter(b: &mut test::Bencher) {
fn fact(n: u32) -> BigUint {
(1..n + 1)
.into_par_iter()
.map(BigUint::from)
.reduce_with(Mul::mul)
.unwrap()
}
let f = factorial(N);
b.iter(|| assert_eq!(fact(test::black_box(N)), f));
}
// end functions copied from rayon-demo
#[bench]
/// Compute the Factorial using rustmp::par_for.
fn factorial_rmp(b: &mut test::Bencher) {
fn fact(n: u32) -> BigUint {
let mut res = BigUint::one();
par_for! {
for i in 2..n + 1, reduction res#*, {
res *= BigUint::from(i);
}
}
res
}
let f = factorial(N);
b.iter(|| assert_eq!(fact(test::black_box(N)), f));
}

View File

@ -73,7 +73,7 @@ macro_rules! critical {
macro_rules! __reduction_operation { macro_rules! __reduction_operation {
($f:ident) => {$f}; ($f:ident) => {$f};
($op:tt) => { ($op:tt) => {
|x, &y| {x $op y} |x, y| {x $op y}
} }
} }