-
Notifications
You must be signed in to change notification settings - Fork 8
/
benchmark.rs
62 lines (52 loc) · 2.2 KB
/
benchmark.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use ::{Digits,ITERATIONS};
use std::collections::HashMap;
use std::sync::Mutex;
use std::sync::atomic::Ordering;
use std::time::Instant;
use std::io;
use std::io::Write;
lazy_static! {
// len => Iterations, Iteration Start, Start Instant (for Duration)
static ref HASHMAP: Mutex<HashMap<u8, (u64, u64, Instant)>> = {
let m = HashMap::new();
Mutex::new(m)
};
}
pub fn report(data: &Digits) {
let len = data.length() as i32;
let mut hm = HASHMAP.lock().unwrap();
if hm.is_empty() {
println!("Benchmark Results\nAttempt :: Category of Range:Iterations, Rating in IPS");
hm.insert(len as u8, (0, 0, Instant::now()));
}
let reader = hm.clone();
if hm.contains_key(&(len as u8)) {
let entry = hm.get_mut(&(len as u8)).unwrap();
let &mut (_, start, duration) = entry;
let iterated = ITERATIONS.load(Ordering::SeqCst) - start.clone() as usize;
let min = reader.keys().min().unwrap();
let max = reader.keys().max().unwrap();
let start_instant = reader.get(min).unwrap().2;
let seconds = Instant::now().duration_since(start_instant).as_secs();
let ips = if seconds == 0 { 0 } else { iterated/seconds as usize };
print!("\x1b[1000D{} :: Category: {}->{}:{}, Rating: {} IPS", data.to_s(), min, max, iterated, ips);
*entry = (iterated as u64, start.clone(), duration.clone());
} else {
// New Entry
let previous = len - 1;
let has_previous = hm.contains_key(&(previous as u8));
let start_iteration = if has_previous { hm.get(&(previous as u8)).unwrap().0 } else { 0 };
let iters = ITERATIONS.load(Ordering::SeqCst);
let instant = Instant::now();
hm.insert(len as u8, (start_iteration, iters as u64, instant));
if has_previous {
let start_iters = if hm.contains_key(&((previous - 1) as u8)) { hm.get(&((previous - 1) as u8)).unwrap().0 } else { 0 };
let iterations = iters - (start_iters as usize);
let start = hm.get(&(previous as u8)).unwrap().2;
let seconds = Instant::now().duration_since(start).as_secs();
let ips = if seconds == 0 { 0 } else { iterations/(seconds as usize) };
println!("\nRange: {}, Iterations: {}, Rating: {} IPS", previous, iterations, ips);
}
}
io::stdout().flush().unwrap();
}