From db978eb1358e3d99a204bf8cdb4582148cf53c4e Mon Sep 17 00:00:00 2001 From: Trivernis Date: Fri, 3 Apr 2020 17:05:50 +0200 Subject: [PATCH] Add bitshift benchmarking --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- src/main.rs | 14 +++++++++++++- src/to_bench.rs | 30 ++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 70ac2e4..413464e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,9 +8,9 @@ checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" [[package]] name = "benchlib-rs" -version = "0.2.2" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8eec9a8d80e5bb03637e22ea0900b8ac12c3b9e21215dfda88347b03f056d5d7" +checksum = "e2b157249b658c600bea79e0ab5366561edf01f6e0dd0ba0075f0ad3945c2420" dependencies = [ "rayon", "termion", diff --git a/Cargo.toml b/Cargo.toml index 3d1efe7..fca0ffb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ opt-level = 1 [dependencies] rayon = "1.3.0" crossbeam-channel = "0.4.2" -benchlib-rs = "0.2.2" +benchlib-rs = "0.3.1" crossbeam-utils = "0.7.2" 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 346211f..4c46215 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,15 +2,27 @@ mod to_bench; +use crate::to_bench::{bitshift_byte, max_f32_multiplication, max_u64_multiplications}; use benchlib::benching::Bencher; use rayon::prelude::*; +pub fn noop() {} + pub fn main() { let mut bencher = Bencher::new(); bencher .set_iterations(10000) .print_settings() - .bench("Dry Run", || {}) + .bench("Empty closure", || {}) + .bench("Empty fn", noop) + .bench("Empty loop to 1000", || for _ in 0..1000 {}) + .bench("u64::MAX x u64::MAX 1000 times", || { + max_u64_multiplications(1000) + }) + .bench("f32::MAX x f32::MAX 1000 times", || { + max_f32_multiplication(1000) + }) + .bench("Bitshift u16 1 byte 1000 times", || bitshift_byte(1000)) .bench("Multiply to 100", || to_bench::multiply_to(100)) .bench("Summation from 0u32 to 10000", || { to_bench::summation_to::(10000) diff --git a/src/to_bench.rs b/src/to_bench.rs index 8db1f31..f995c95 100644 --- a/src/to_bench.rs +++ b/src/to_bench.rs @@ -3,9 +3,11 @@ use crossbeam_utils::sync::WaitGroup; use num_cpus; use num_traits::{PrimInt, Unsigned}; use rayon::prelude::*; +use std::f32; use std::sync::mpsc::channel; use std::sync::{Arc, Mutex}; use std::thread; +use std::u64; pub fn start_stop_thread() { let handle = thread::spawn(|| { @@ -123,3 +125,31 @@ pub fn start_and_wait_for_num_cpu_threads() { } wg.wait(); } + +pub fn max_f32_multiplication(times: usize) -> f64 { + let mut result = 0f64; + for _ in 0..times { + result = f32::MAX as f64 * f32::MAX as f64; + } + + result +} + +pub fn max_u64_multiplications(times: usize) -> u128 { + let mut result = 0u128; + for _ in 0..times { + result = u64::MAX as u128 * u64::MAX as u128 + } + + result +} + +pub fn bitshift_byte(times: usize) -> u16 { + let mut result = 0u16; + for _ in 0..(times / 2) { + result = result << 8; + result = result >> 8; + } + + result +}