codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🦀

Rust

44 / 58 topics
44Benchmarking45Optimization
Tutorials/Rust/Benchmarking
🦀Rust

Benchmarking

Updated 2026-05-15
10 min read

Benchmarking

Introduction

In the world of software development, performance is a critical factor. Whether you're building high-performance systems or simply optimizing your applications for better responsiveness, benchmarking is an essential tool. Rust, with its focus on safety and speed, provides powerful tools to measure and optimize performance. In this tutorial, we'll explore how to use Criterion, a popular benchmarking library for Rust, to analyze and improve the performance of your code.

Concept

Benchmarking involves measuring the execution time or resource usage of specific parts of your code. This helps you identify bottlenecks and areas that need optimization. Criterion is a high-performance benchmarking library specifically designed for Rust. It provides detailed statistical analysis and generates reports that help you understand the performance characteristics of your code.

Criterion works by running benchmarks multiple times, collecting data on execution time, and then analyzing this data to provide accurate and reliable results. It also includes features like automatic outlier detection and noise reduction, which are crucial for obtaining meaningful benchmark results.

Examples

Setting Up Criterion

To use Criterion in your Rust project, you first need to add it as a dependency. Open your Cargo.toml file and add the following under [dependencies]:

toml
1[dependencies]
2criterion = "0.3"

Next, include Criterion in your main.rs or any other benchmarking module by adding the following use statement:

Rust
1use criterion::Criterion;

Writing a Simple Benchmark

Let's write a simple benchmark to measure the performance of a function that calculates the factorial of a number. First, define the function you want to benchmark:

Rust
1fn factorial(n: u64) -> u64 {
2 if n == 0 || n == 1 {
3 1
4 } else {
5 n * factorial(n - 1)
6 }
7}

Now, create a benchmark function using Criterion:

Rust
1fn criterion_benchmark(c: &mut Criterion) {
2 c.bench_function("factorial", |b| b.iter(|| factorial(20)));
3}
4
5criterion_group!(benches, criterion_benchmark);
6criterion_main!(benches);

In this example, c.bench_function registers a benchmark named "factorial". The closure passed to b.iter specifies the code to be benchmarked. Here, we're calling the factorial function with an input of 20.

Running the Benchmark

To run your benchmarks, use the following command in your terminal:

Terminal
cargo bench

Cargo will compile your project and execute the benchmarks. The output will include detailed performance statistics for each benchmark, such as mean execution time, standard deviation, and other relevant metrics.

Output
running 1 test
test benches::criterion_benchmark ... bench:      20,000 ns/iter (+/- 5,000)
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured; 0 filtered out; finished in 0.03s

Analyzing the Results

Criterion generates a detailed report after running the benchmarks. You can find this report in the target/criterion directory. Open the report/index.html file in your browser to view an interactive HTML report that provides insights into the performance characteristics of your code.

The report includes various charts and graphs, such as:

  • Mean Execution Time: The average time taken for each iteration.
  • Standard Deviation: A measure of how much the execution time varies between iterations.
  • Noise Percentage: Indicates the level of noise in the benchmark results.
  • Outliers: Any iterations that are significantly slower or faster than others.

Advanced Benchmarking Techniques

Criterion offers several advanced features to help you write more accurate and meaningful benchmarks:

  • Parameterized Benchmarks: You can benchmark functions with different input parameters by using c.bench_with_input.
Rust
1c.bench_with_input("factorial", &10, |b, &n| b.iter(|| factorial(n)));
  • Baseline Benchmarks: Compare the performance of different implementations by using c.bench_function_over_inputs.
Rust
1c.bench_function_over_inputs("factorial", vec![10, 20, 30], |b, &n| b.iter(|| factorial(n)));
  • Custom Benchmarks: You can create custom benchmarks by implementing the Benchmark trait.
Rust
1use criterion::{black_box, Benchmark, Criterion};
2
3fn custom_benchmark(c: &mut Criterion) {
4 let mut group = c.benchmark_group("custom");
5 for size in [10, 20, 30].iter() {
6 group.bench_with_input(BenchmarkId::new("factorial", *size), size, |b, &n| b.iter(|| factorial(n)));
7 }
8 group.finish();
9}
10
11criterion_group!(benches, custom_benchmark);
12criterion_main!(benches);

What's Next?

Now that you've learned how to use Criterion for benchmarking Rust code, the next step is to focus on optimization. Based on the insights gained from your benchmarks, you can make informed decisions about optimizing your code. This might involve:

  • Refactoring algorithms for better performance.
  • Using more efficient data structures.
  • Minimizing memory allocations and deallocations.
  • Leveraging parallelism and concurrency where appropriate.

By combining benchmarking with optimization techniques, you can significantly improve the performance of your Rust applications.

Remember, benchmarking is an iterative process. Continuously measure and optimize your code to ensure it meets your performance requirements.


PreviousIntegration TestsNext Optimization

Recommended Gear

Integration TestsOptimization