diff --git a/Cargo.lock b/Cargo.lock index 16f2d71..70ac2e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,7 @@ dependencies = [ "benchlib-rs", "crossbeam-channel", "crossbeam-utils", + "num-traits", "num_cpus", "rayon", ] @@ -132,6 +133,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num-traits" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.12.0" diff --git a/Cargo.toml b/Cargo.toml index 452fe08..3d1efe7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,5 @@ rayon = "1.3.0" crossbeam-channel = "0.4.2" benchlib-rs = "0.2.2" crossbeam-utils = "0.7.2" -num_cpus = "1.0" \ No newline at end of file +num_cpus = "1.0" +num-traits = "0.2.11" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1efffaa..346211f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod to_bench; use benchlib::benching::Bencher; +use rayon::prelude::*; pub fn main() { let mut bencher = Bencher::new(); @@ -11,12 +12,24 @@ pub fn main() { .print_settings() .bench("Dry Run", || {}) .bench("Multiply to 100", || to_bench::multiply_to(100)) - .bench("Summation from 0u128 to 1000", || { - to_bench::summation_to(1000) + .bench("Summation from 0u32 to 10000", || { + to_bench::summation_to::(10000) }) + .bench("Summation from 0u64 to 10000", || { + to_bench::summation_to::(10000) + }) + .compare() + .bench("Summation from 0u128 to 10000", || { + to_bench::summation_to::(10000) + }) + .compare() + .bench("Parallel summation using rayon from 0u128 to 10000", || { + (0u128..10000).into_par_iter().sum::() + }) + .compare() .bench( - "Parallel summation with arc mutex from 0u128 to 1000", - || to_bench::summation_using_mutex(1000), + "Parallel summation with arc mutex from 0u128 to 10000", + || to_bench::summation_using_mutex(10000), ) .compare() .set_iterations(1000) diff --git a/src/to_bench.rs b/src/to_bench.rs index 58b9c1f..37f428b 100644 --- a/src/to_bench.rs +++ b/src/to_bench.rs @@ -1,6 +1,7 @@ use crossbeam_channel::unbounded; use crossbeam_utils::sync::WaitGroup; use num_cpus; +use num_traits::{PrimInt, Unsigned}; use rayon::prelude::*; use std::sync::mpsc::channel; use std::sync::{Arc, Mutex}; @@ -13,10 +14,12 @@ pub fn start_stop_thread() { handle.join().unwrap(); } -pub fn summation_to(end: u128) -> u128 { - let mut res = 0u128; - for i in 0u128..end { - res += i; +pub fn summation_to(end: T) -> T { + let mut res: T = T::zero(); + let mut i = T::zero(); + while i < end { + res = res + i; + i = i + T::one(); } res }