Expand description
You’ve probably heard of print debugging, but maybe not the lesser known member of the print family: print-optimization. Sometimes it’s interesting to measure the time some part of your code uses, but you won’t set up everything you need for profiling your entire program (or you don’t have that option in the environment you’re working in). Doing this in Rust requires some boilerplate at the moment, especially if you want to print out an easily readable output that you can navigate. directly to the relevant lines of code from. This crate aims to make this easier to do: Here’s an example:
use print_perf::*;
fn add(a: i32, b: i32) -> i32 {
sleep(Duration::from_millis(100));
a + b
}
fn main() {
let add_p = perf!("add fn");
let result = add(4, 4);
add_p.end();
// ^-- prints: 0.100140446 (add fn) @ [src/main.rs:9]
assert_eq!(result, 8);
}
§Example with splits
use print_perf::*;
fn add(a: i32, b: i32) -> i32 {
sleep(Duration::from_millis(100));
a + b
}
fn main() {
let p = perf!("add fn");
let _result = add(4, 4);
p.split("add");
let _div = _result / 2;
p.split("div");
p.end();
}
You can use two methods to measure the elapsed time:
- Lap: measures elapsed time from the last lap (or the starting point if it’s the first lap)
- Split: measures elapsed time from the starting point where you call it in your code
§Stability
The exact output printed by this macro should not be relied upon and is subject to future changes.
§Panics
Panics if writing to io::stderr
fails.
Macros§
- perf
- Se crate documentation for example on how to use
Structs§
- Perf
- This is what you get returned from the macro. You probably won’t create this directly.