How to do benchmarking in Rust

In Rust, benchmarking is the process of measuring the performance of Rust code. This can be done to compare the performance of different versions of the same code, or to compare Rust code to the code written in other languages.

Using std::time::Instant

If you prefer to perform benchmarking without external dependencies, you can use Rust's built-in std::time::Instant to measure the execution time of your code. Here's an example of how you can do it:

use std::time::Instant;
fn sort_benchmark() {
let mut data = vec![5, 4, 3, 2, 1];
let start_time = Instant::now();
// Code to benchmark
data.sort();
let elapsed_time = start_time.elapsed();
println!("Elapsed time: {:?}", elapsed_time);
}
fn main() {
sort_benchmark();
}

Explanation

Line 1: The Instant struct from the std::time module is imported.

Line 3: A function named sort_benchmark is defined.

Line 4: A mutable vector named data is created and initialized.

Line 6: An instance of the Instant struct named start_time is created, capturing the current time.

Line 11: The elapsed method is called on the start_time instance, returning a Duration object representing the elapsed time.

Line 13: The elapsed time stored in the elapsed_time variable is printed to the console using the println! macro.

Line 18: The sort_benchmark function is called to execute the benchmarking code.

Using criterion crate

The criterion code provides a framework for reliable benchmarking by automatically running multiple iterations of our code, measuring the execution time, and providing statistical analysis of the results. Here's a step-by-step guide on how to use criterion :

  1. Setting up the project: Create a new Rust project or navigate to an existing project directory in your terminal.

  2. Add criterion as a dependency: Open the Cargo.toml file of your project and add the criterion= <version> to the [dependencies] section. Shown below, is an sample Cargo.toml file.

[package]
name = "my_project"
version = "0.1.0"
edition = "2021"
[dependencies]
criterion= <version>
[[bin]]
name = "my_project"
path = "benchmark.rs"
cargo.toml contents

Note: Replace <version> with the specific version of criterion you want to use.

  1. Write the benchmark code: Create a new file, for example, benchmarks.rs, where you'll write your benchmark code. In this file, you'll define one or more benchmark functions using the criterion_group and criterion_main macros.

use criterion::{criterion_group, criterion_main, Criterion};
fn my_benchmark_function(c: &mut Criterion) {
c.bench_function("my_benchmark", |b| {
b.iter(|| {
// Code to be benchmarked
});
});
}
criterion_group!(benches, my_benchmark_function);
criterion_main!(benches);

Explanation

Line 1: The necessary crates criterion_group, criterion_main, and Criterion for benchmarking are imported.

Line 3: A function named my_benchmark_function is defined, which takes a mutable reference to a Criterion object c as an argument. This function will be used to define the benchmarking code.

Line 4: The c.bench_function method is called on the criterion object c. It takes two arguments - a string identifier for the benchmark, in this case, "my_benchmark", and a closureA feature in Rust that allows a function to capture variables from its surrounding environment, including those defined in outer scopes. that defines the code to be benchmarked.

Line 5: Within the closure, the b.iter method is called on the benchmark object b. It sets up an iteration loop to run the code multiple times for accurate measurements.

Line 6: Inside the b.iter loop, the code to be benchmarked is written. This is the part that will be measured for performance.

Line 10: The criterion_group macro is used to define a benchmark group named "benches". It takes two arguments - the name of the group and a list of benchmark functions to include in the group.

Line 11: The my_benchmark_function is included in the benchmark group.

Line 13: The criterion_main macro is used to run the benchmark group. It takes a single argument, which is the name of the benchmark group.

  1. Run the benchmark: Open your terminal, navigate to your project directory, and run the following command to execute the benchmark:

cargo bench
Running the bench command

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved