Move to_bench to separate file

master
Trivernis 5 years ago
parent 8d87b7415e
commit 580b956b54

4
Cargo.lock generated

@ -8,9 +8,9 @@ checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
[[package]] [[package]]
name = "benchlib-rs" name = "benchlib-rs"
version = "0.2.0" version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b2fbf36bb2cf3eeb3f83dff4d5ba3083f08048173567b0c8a391a55591eb4af" checksum = "8eec9a8d80e5bb03637e22ea0900b8ac12c3b9e21215dfda88347b03f056d5d7"
dependencies = [ dependencies = [
"rayon", "rayon",
"termion", "termion",

@ -9,4 +9,4 @@ edition = "2018"
[dependencies] [dependencies]
rayon = "1.3.0" rayon = "1.3.0"
crossbeam-channel = "0.4.2" crossbeam-channel = "0.4.2"
benchlib-rs = "0.2.0" benchlib-rs = "0.2.2"

@ -1,101 +1,33 @@
#![allow(dead_code)] #![allow(dead_code)]
mod to_bench;
use benchlib::benching::Bencher; use benchlib::benching::Bencher;
pub fn main() { pub fn main() {
let mut bencher = Bencher::new(); let mut bencher = Bencher::new();
bencher bencher
.set_iterations(10000) .set_iterations(100000000)
.bench("Multiply to 100", || to_test::multiply_to(100)) .print_settings()
.bench("Spawn and Stop thread", || to_test::start_stop_thread()) .bench("Multiply to 100", || to_bench::multiply_to(100))
.bench("MPSC channel 1000 u128", || to_test::send_mpsc_channel()) .bench("Summation from 0u128 to 1000000", || {
.bench("MPMC channel 1000 u128", || to_test::send_mpmc_channel()) 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() .compare()
.bench("Largest prime until 1000000", || { .bench("Largest prime until 1000000", || {
to_test::largest_prime(1000000) to_bench::largest_prime(1000000)
}) })
.bench("Largest prime parallel until 1000000", || { .bench("Largest prime parallel until 1000000", || {
to_test::largest_prime_par(1000000) to_bench::largest_prime_par(1000000)
}) })
.compare(); .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::<u128>();
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::<u128>();
let handle = thread::spawn(move || for _ in tx {});
for i in 0..1000 {
rx.send(i).unwrap();
}
std::mem::drop(rx);
handle.join().unwrap();
}
}

@ -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::<u128>();
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::<u128>();
let handle = thread::spawn(move || for _ in tx {});
for i in 0..1000 {
rx.send(i).unwrap();
}
std::mem::drop(rx);
handle.join().unwrap();
}
Loading…
Cancel
Save