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.
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 benchmarkdata.sort();let elapsed_time = start_time.elapsed();println!("Elapsed time: {:?}", elapsed_time);}fn main() {sort_benchmark();}
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.
criterion
crateThe 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
:
Setting up the project: Create a new Rust project or navigate to an existing project directory in your terminal.
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"
Note: Replace
<version>
with the specific version ofcriterion
you want to use.
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);
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
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.
Run the benchmark: Open your terminal, navigate to your project directory, and run the following command to execute the benchmark:
cargo bench
Free Resources