diff --git a/Cargo.lock b/Cargo.lock index 2366eeb..714ec9e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,9 +8,9 @@ checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" [[package]] name = "benchlib-rs" -version = "0.2.0" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b2fbf36bb2cf3eeb3f83dff4d5ba3083f08048173567b0c8a391a55591eb4af" +checksum = "8eec9a8d80e5bb03637e22ea0900b8ac12c3b9e21215dfda88347b03f056d5d7" dependencies = [ "rayon", "termion", diff --git a/Cargo.toml b/Cargo.toml index e2aff78..62c5707 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,4 @@ edition = "2018" [dependencies] rayon = "1.3.0" crossbeam-channel = "0.4.2" -benchlib-rs = "0.2.0" \ No newline at end of file +benchlib-rs = "0.2.2" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index fc9734a..20f52cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,101 +1,33 @@ #![allow(dead_code)] +mod to_bench; + use benchlib::benching::Bencher; pub fn main() { let mut bencher = Bencher::new(); bencher - .set_iterations(10000) - .bench("Multiply to 100", || to_test::multiply_to(100)) - .bench("Spawn and Stop thread", || to_test::start_stop_thread()) - .bench("MPSC channel 1000 u128", || to_test::send_mpsc_channel()) - .bench("MPMC channel 1000 u128", || to_test::send_mpmc_channel()) + .set_iterations(100000000) + .print_settings() + .bench("Multiply to 100", || to_bench::multiply_to(100)) + .bench("Summation from 0u128 to 1000000", || { + to_bench::summation_to_1000000() + }) + .set_iterations(1000) + .print_settings() + .bench("Spawn and Stop thread", || to_bench::start_stop_thread()) + .bench("MPSC channel transmit 1000x u128", || { + to_bench::send_mpsc_channel() + }) + .bench("MPMC channel transmit 1000x u128", || { + to_bench::send_mpmc_channel() + }) .compare() .bench("Largest prime until 1000000", || { - to_test::largest_prime(1000000) + to_bench::largest_prime(1000000) }) .bench("Largest prime parallel until 1000000", || { - to_test::largest_prime_par(1000000) + to_bench::largest_prime_par(1000000) }) .compare(); } - -mod to_test { - - use crossbeam_channel::unbounded; - use rayon::prelude::*; - use std::sync::mpsc::channel; - use std::thread; - - pub fn start_stop_thread() { - let handle = thread::spawn(|| { - return; - }); - handle.join().unwrap(); - } - - pub fn multiply_to(end: usize) -> f64 { - let mut result = 0f64; - for i in 2..end { - result = (result * i as f64) / (i - 1) as f64; - } - - result - } - - pub fn largest_prime(end: u128) -> u128 { - let mut last_prime = 2; - for i in (2u128..end).step_by(2) { - let mut is_prime = true; - for j in 2..(i as f64).sqrt().ceil() as u128 { - if i % j == 0 { - is_prime = false; - break; - } - } - if is_prime { - last_prime = i; - } - } - - last_prime - } - - pub fn largest_prime_par(end: u128) -> u128 { - (2u128..((end as f64) / 2f64).ceil() as u128) - .into_par_iter() - .filter(|number| { - let num = number * 2; - for i in 2..(num as f64).sqrt().ceil() as u128 { - if num % i == 0 { - return false; - } - } - - true - }) - .max() - .unwrap() - * 2 - } - - pub fn send_mpsc_channel() { - let (rx, tx) = channel::(); - let handle = thread::spawn(move || for _ in tx {}); - for i in 0..1000 { - rx.send(i).unwrap(); - } - std::mem::drop(rx); - handle.join().unwrap(); - } - - pub fn send_mpmc_channel() { - let (rx, tx) = unbounded::(); - let handle = thread::spawn(move || for _ in tx {}); - for i in 0..1000 { - rx.send(i).unwrap(); - } - std::mem::drop(rx); - handle.join().unwrap(); - } -} diff --git a/src/to_bench.rs b/src/to_bench.rs new file mode 100644 index 0000000..20f163c --- /dev/null +++ b/src/to_bench.rs @@ -0,0 +1,84 @@ +use crossbeam_channel::unbounded; +use rayon::prelude::*; +use std::sync::mpsc::channel; +use std::thread; + +pub fn start_stop_thread() { + let handle = thread::spawn(|| { + return; + }); + handle.join().unwrap(); +} + +pub fn summation_to_1000000() -> u128 { + let mut res = 0u128; + for i in 0u128..1000000 { + res += i; + } + res +} + +pub fn multiply_to(end: usize) -> f64 { + let mut result = 0f64; + for i in 2..end { + result = (result * i as f64) / (i - 1) as f64; + } + + result +} + +pub fn largest_prime(end: u128) -> u128 { + let mut last_prime = 2; + for i in (2u128..end).step_by(2) { + let mut is_prime = true; + for j in 2..(i as f64).sqrt().ceil() as u128 { + if i % j == 0 { + is_prime = false; + break; + } + } + if is_prime { + last_prime = i; + } + } + + last_prime +} + +pub fn largest_prime_par(end: u128) -> u128 { + (2u128..((end as f64) / 2f64).ceil() as u128) + .into_par_iter() + .filter(|number| { + let num = number * 2; + for i in 2..(num as f64).sqrt().ceil() as u128 { + if num % i == 0 { + return false; + } + } + + true + }) + .max() + .unwrap() + * 2 +} + +pub fn send_mpsc_channel() { + let (rx, tx) = channel::(); + let handle = thread::spawn(move || for _ in tx {}); + for i in 0..1000 { + rx.send(i).unwrap(); + } + std::mem::drop(rx); + handle.join().unwrap(); +} + +pub fn send_mpmc_channel() { + let (rx, tx) = unbounded::(); + let handle = thread::spawn(move || for _ in tx {}); + for i in 0..1000 { + rx.send(i).unwrap(); + } + std::mem::drop(rx); + handle.join().unwrap(); +}