Avatar ·

How to properly measure performance (benchmarking) of Rust programs?

Hello everyone! I'm a beginner in Rust and trying to understand how to properly measure the performance of my programs. In other languages (e.g., Python or JavaScript), I just used built-in timers or modules, but in Rust it turned out to be more complicated. I wrote a simple function that sums elements in a vector, and I want to know how long it takes to execute: ```rust fn sum_vec(vec: &Vec) -> i32 { let mut sum = 0; for i in vec { sum += i; } sum } fn main() { let data = vec![1; 1_000_000]; // How to measure the execution time of sum_vec(&data)? println!("Sum: {}", sum_vec(&data)); } ``` I tried using `std::time::Instant`, but I get strange results: sometimes 0 microseconds, sometimes widely varying values on repeated runs.