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
pub fn ops(nops: u32, threads: u32, mut f: impl FnMut(u32, u32)) { if threads != 1 { panic!("support for multiple threads is not supported yet"); } let spread = if nops / threads * threads < nops { nops / threads + 1 } else { nops / threads }; let mut i = 0; let mut remaining = nops; let now = std::time::Instant::now(); for _ in 0..threads { let take = if remaining > spread { spread } else { remaining }; remaining -= take; for _ in 0..take { f(i, 0); i += 1; } } let elapsed = now.elapsed(); let nano = elapsed.as_nanos(); let ms = elapsed.as_millis(); let secs = nano as f64 / 1e9; let ops_sec = ((nops as f64) / secs) as u64; let nsop = (nano as f64 / nops as f64) as u64; print!("{} ops", commaize(nops as u64)); if threads != 1 { print!(" over {} threads", threads); } println!(" in {}ms, {}/sec, {} ns/op", ms, commaize(ops_sec), nsop); } fn commaize(num: u64) -> String { let s1: String = format!("{}", num); let b = s1.as_bytes(); let mut s2 = String::new(); let mut i = (s1.len() - 1) as isize; let mut j = 0; while i >= 0 { if j % 3 == 0 && j != 0 { s2 = ",".to_owned() + &s2; } s2 = format!("{}", b[i as usize] as char) + &s2; i -= 1; j += 1; } s2 }