So I'm still kinda green in Rust, but coming from Python I find this scenario very confusing in general.
I like Python because it's very easy if you want to time a block of code or just a function call:
print(timeit('a = "hee hee la le dah"; my_awesome_fn()', number = 1_000, globals=globals()))
Then just call python script.py
or better yet just use the green "run" button in the IDE and you can call the script. But I'm having trouble finding functional equivalent in Rust.
I know there is concept in Rust ecosystem called benchmarking and some libs like criterion
exist for this purpose. The problem is that I know nothing about advanced math and statistics (can treat me like a clueless moron essentially) and I doubt I can benefit a lot from a framework or harness such as this.
So I am simply just curious how can I use tests
in cargo to test a block of code in Rust or better yet even a function call.
For example assume I have similar function in rust that I want to call multiple times and then check how does performance change etc:
pub fn my_awesome_fn() {
trace!("getting ready to do something cool...");
std::thread::sleep(std::time::Duration::from_millis(500));
info!("finished!");
}
how can I simply just time this func my_awesome_fn
in rust? I guess i'm looking for an equivalent like timeit
in python or something similar. Ideally it should be striaghtforward to use and assume i don't know anything about what I'm doing. I'm curious if there's an existing library or framework I can leverage for this purposes.
From: https://rust-lang-nursery.github.io/rust-cookbook/datetime/duration.html
use std::time::{Duration, Instant};
fn main() {
let start = Instant::now();
expensive_function();
let duration = start.elapsed();
println!("Time elapsed in expensive_function() is: {:?}", duration);
}