Disclaimer: I've never used timeit
A very quick answer solution is to write a function like:
fn timeit<F: Fn() -> T, T>(f: F) -> T {
let start = SystemTime::now();
let result = f();
let end = SystemTime::now();
let duration = end.duration_since(start).unwrap();
println!("it took {} seconds", duration.as_secs());
result
}
which you can use to "wrap" another function call:
fn main() {
let x = timeit(|| my_expensive_function());
}
However, if you're trying to understand the time a function takes for the purpose of performance optimizations, this approach is likely too crude.
The problem is that I know nothing about advanced math and statistics
That's arguably one of the main advantages of criterion
, it "abstracts the maths away", in a sense.
It uses statistical approaches to give you a better insight into whether differences between benchmarking runs are a product of "randomness", or whether there is a meaningful difference between the code on each run.
To the end user, it essentially gives you a report saying either "a significant change was observed" or "no significant change was observed". It does far more than that, but to fully grasp its capabilities, it might be worth reading up on "hypothesis testing".
If you're OK using nightly Rust, you can also use #[bench]
tests:
#![feature(test)]
extern crate test;
#[bench]
fn bench_my_func(b: &mut Bencher) {
b.iter(|| my_func(black_box(100));
}
which you can run with cargo bench
. These are a bit easier to set up than criterion
, but do less of the interesting stats (i.e. you'll have to do it yourself), but they're a very "quick and dirty" way to get a feel for the runtime of your code.
A word of warning, benchmarking code is hard. You may be surprised at what is actually going on under the hood, and you may find yourself benchmarking the wrong thing.
Common "gotchas" are:
rustc
can generally identify "useless" code, and simply skip calculating it. The black_box
function can be used to hide the meaning of some data from the optimizer, though it is not without its own overhead
- in a similar vein, LLVM does some slightly spooky optimizations relating to polynomials for example. You might find that your function call is being optimized away into a constant/simple arithmetic. In some cases, this is great! You've written your function in such a way that LLVM can reduce it to something trivial. In other cases, you're now just benchmarking the multiplication instruction on your CPU, which is unlikely to be what you want. Use your best judgement
- benchmarking the wrong thing - some things are significantly more expensive than others, in ways that might seem odd to someone with a python background. For example, cloning a
String
(even a very short one) might be 2-3 orders of magnitude slower than finding the first character. Consider the following:
fn str_len(s: String) -> usize {
s.len()
}
#[bench]
fn bench_str_len(b: &mut Bencher) {
let s = String::from("hello");
b.iter(|| str_len(s.clone()));
}
Because String::clone
involves a heap allocation, but s.len()
is just a field access, it will dominate the results. Instead, if str_len
took a &str
, it would become more representative (though this is a contrived case).
TLDR Be careful what your benchmark code is doing. The Rust Playground's "view assembly" tool (or godbolt.org) are your friends. You don't need to be an assembly expert, but it can help give you some idea what's going on under the hood