Created
August 22, 2023 23:31
-
-
Save andreievg/71bb0be84c41d48b181a01a7e6bb3dcb to your computer and use it in GitHub Desktop.
Rust bench helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn bench_point(identifier: &str) { | |
BENCH_POINTS.lock().unwrap().push(BenchPoint { | |
identifier: identifier.to_string(), | |
time: Utc::now(), | |
}) | |
} | |
pub static BENCH_POINTS: Mutex<Vec<BenchPoint>> = Mutex::new(Vec::new()); | |
// ideally this would be a macro to capture file and location | |
pub fn bench_point(identifier: &str) { | |
BENCH_POINTS.lock().unwrap().push(BenchPoint { | |
identifier: identifier.to_string(), | |
time: Utc::now(), | |
}) | |
} | |
pub fn bench_clear(identifier: &str) { | |
BENCH_POINTS.lock().unwrap().push(BenchPoint { | |
identifier: identifier.to_string(), | |
time: Utc::now(), | |
}) | |
} | |
pub fn bench_results() { | |
let mut bench_points = BENCH_POINTS.lock().unwrap(); | |
bench_points.sort_by(|a, b| a.time.cmp(&b.time)); | |
let Some(start_time) = bench_points.first() else { | |
return; | |
}; | |
let mut previous_time = start_time.time; | |
for BenchPoint { identifier, time } in bench_points.iter() { | |
let diff_from_start = *time - start_time.time; | |
let diff_from_previous = *time - previous_time; | |
println!("{identifier: <30}\t{diff_from_start: <10}\t{diff_from_previous: <10}"); | |
previous_time = *time; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use:
There is soo much more that can be improved, but was sufficient for quick use case I had.
Improvements: