Hi!
I've finally published my first crate to crates.io: https://github.com/matklad/elapsed
It provides a single function, measureTime
, which you can use like this:
let (elapsed, sum) = measure_time(|| {
(0..10_000).sum::<u64>()
});
println!("elapsed = {}", elapsed); // prints = 227.81 μs
println!("sum = {}", sum);
The return type wraps std::Duration
to provide a useful Display
implementation, so that you can concentrate on finding the bottlenecks in the code code, and not on the extracting elapsed milliseconds from a Duration
.
3 Likes
Nice!
Any reason you don't use Instant::elapsed()
?
If you're taking requests, user and system time would also be nice to have. Instant
is basically just "real" time. But you'd have to venture outside of std::time
for these.
Thanks! I've totally forgotten that it exists! [quote="cuviper, post:2, topic:10204"]
If you're taking requests, user and system time would also be nice to have. Instant is basically just "real" time. But you'd have to venture outside of std::time for these.
[/quote]
Hm, that's an interesting idea. I certainly wish to profile(|| { ... })
a block of code sometimes to get different performance measures/find hotspots. I wonder if I can perf stat
the process from itself...
Ooh, perf
is more hardcore than I was thinking, but you could do a lot with that.
I just meant something like getrusage
RUSAGE_THREAD
. RUSAGE_SELF
and RUSAGE_CHILDREN
are also useful, with the caveat that they can't be contained to measuring just activity initiated by the closure.
1 Like