mirror of https://github.com/xythrez/RustMP.git
Add factorial benchmark
This commit is contained in:
parent
8227ae5c65
commit
ba675a12c2
|
@ -14,3 +14,4 @@ rand = "0.8.3"
|
|||
rayon = "1.5.0"
|
||||
hwloc2 = "2.2.0"
|
||||
lazy_static = "1.4.0"
|
||||
num = "0.3"
|
||||
|
|
10
README.md
10
README.md
|
@ -29,8 +29,14 @@ following command:
|
|||
$ cargo run --release --bin <testname>
|
||||
```
|
||||
|
||||
Additional comparison programs are located in `omp/`, where C comparison
|
||||
benchmarks can be found.
|
||||
C comparison benchmarks can be found in `omp/`.
|
||||
|
||||
Additional benchmarks are located in `benches`. These require Rust nightly and
|
||||
can be run with the following command:
|
||||
|
||||
```
|
||||
$ cargo +nightly bench
|
||||
```
|
||||
|
||||
## Known issues
|
||||
|
||||
|
|
|
@ -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));
|
||||
}
|
|
@ -73,7 +73,7 @@ macro_rules! critical {
|
|||
macro_rules! __reduction_operation {
|
||||
($f:ident) => {$f};
|
||||
($op:tt) => {
|
||||
|x, &y| {x $op y}
|
||||
|x, y| {x $op y}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue