bitsliced_op/
benchmark.rs1use std::time::{Duration, Instant};
2
3struct BenchmarkResult {
4 runs: u64,
5 duration: Duration,
6}
7
8pub fn benchmark<F>(name: &str, runs: u64, warmup: u64, parallel_count: u64, mut func: F)
9where
10 F: FnMut(),
11{
12 for _ in 0..warmup {
13 func();
14 }
15 let start = Instant::now();
16 for _ in 0..runs {
17 func();
18 }
19 let duration = start.elapsed();
20 let result = BenchmarkResult { runs, duration };
21 let operations_per_second =
22 (result.runs as f64 / result.duration.as_secs_f64()) * parallel_count as f64;
23 print_report(name, operations_per_second);
24}
25
26fn print_report(name: &str, hashes_per_second: f64) {
27 println!(
28 "{} ran at {}",
29 name,
30 format_operations_per_second(hashes_per_second)
31 );
32}
33
34fn format_operations_per_second(hashes_per_second: f64) -> String {
35 let gh_delimiter = 1_000_000_000.0;
36 let mh_delimiter = 1_000_000.0;
37 let kh_delimiter = 1_000.0;
38 if hashes_per_second > gh_delimiter {
39 format!("{:.2}GH/s", hashes_per_second / gh_delimiter)
40 } else if hashes_per_second > mh_delimiter {
41 format!("{:.2}MH/s", hashes_per_second / mh_delimiter)
42 } else if hashes_per_second > kh_delimiter {
43 format!("{:.2}KH/s", hashes_per_second / kh_delimiter)
44 } else {
45 format!("{:.2}H/s", hashes_per_second)
46 }
47}