README.md
1<h1 align="center">Criterion.<span></span>rs</h1>
2
3<div align="center">Statistics-driven Microbenchmarking in Rust</div>
4
5<div align="center">
6 <a href="https://bheisler.github.io/criterion.rs/book/getting_started.html">Getting Started</a>
7 |
8 <a href="https://bheisler.github.io/criterion.rs/book/index.html">User Guide</a>
9 |
10 <a href="https://bheisler.github.io/criterion.rs/criterion/">Master API Docs</a>
11 |
12 <a href="https://docs.rs/crate/criterion/">Released API Docs</a>
13 |
14 <a href="https://github.com/bheisler/criterion.rs/blob/master/CHANGELOG.md">Changelog</a>
15</div>
16
17<div align="center">
18 <a href="https://github.com/bheisler/criterion.rs/actions/workflows/ci.yaml">
19 <img src="https://img.shields.io/github/checks-status/rgeometry/rgeometry/main?label=tests&logo=github" alt="GitHub branch checks state">
20 </a>
21 |
22 <a href="https://ci.appveyor.com/project/bheisler/criterion-rs-vt9fl">
23 <img src="https://ci.appveyor.com/api/projects/status/4255ads9ctpupcl2?svg=true" alt="Appveyor">
24 </a>
25 |
26 <a href="https://crates.io/crates/criterion">
27 <img src="https://img.shields.io/crates/v/criterion.svg" alt="Crates.io">
28 </a>
29</div>
30
31Criterion.<span></span>rs helps you write fast code by detecting and measuring performance improvements or regressions, even small ones, quickly and accurately. You can optimize with confidence, knowing how each change affects the performance of your code.
32
33## Table of Contents
34- [Table of Contents](#table-of-contents)
35 - [Features](#features)
36 - [Quickstart](#quickstart)
37 - [Goals](#goals)
38 - [Contributing](#contributing)
39 - [Compatibility Policy](#compatibility-policy)
40 - [Maintenance](#maintenance)
41 - [License](#license)
42 - [Related Projects](#related-projects)
43 - [Criterion.rs Extensions](#criterionrs-extensions)
44
45### Features
46
47- __Statistics__: Statistical analysis detects if, and by how much, performance has changed since the last benchmark run
48- __Charts__: Uses [gnuplot](http://www.gnuplot.info/) to generate detailed graphs of benchmark results
49- __Stable-compatible__: Benchmark your code without installing nightly Rust
50
51### Quickstart
52
53In order to generate plots, you must have [gnuplot](http://www.gnuplot.info/) installed. See the gnuplot website for installation instructions. See [Compatibility Policy](#compatibility-policy) for details on the minimum supported Rust version.
54
55To start with Criterion.<span></span>rs, add the following to your `Cargo.toml` file:
56
57```toml
58[dev-dependencies]
59criterion = "0.3"
60
61[[bench]]
62name = "my_benchmark"
63harness = false
64```
65
66Next, define a benchmark by creating a file at `$PROJECT/benches/my_benchmark.rs` with the following contents:
67
68```rust
69use criterion::{black_box, criterion_group, criterion_main, Criterion};
70
71fn fibonacci(n: u64) -> u64 {
72 match n {
73 0 => 1,
74 1 => 1,
75 n => fibonacci(n-1) + fibonacci(n-2),
76 }
77}
78
79fn criterion_benchmark(c: &mut Criterion) {
80 c.bench_function("fib 20", |b| b.iter(|| fibonacci(black_box(20))));
81}
82
83criterion_group!(benches, criterion_benchmark);
84criterion_main!(benches);
85```
86
87Finally, run this benchmark with `cargo bench`. You should see output similar to the following:
88
89```
90 Running target/release/deps/example-423eedc43b2b3a93
91fib 20 time: [26.029 us 26.251 us 26.505 us]
92Found 11 outliers among 99 measurements (11.11%)
93 6 (6.06%) high mild
94 5 (5.05%) high severe
95```
96
97See the [Getting Started](https://bheisler.github.io/criterion.rs/book/getting_started.html) guide for more details.
98
99### Goals
100
101The primary goal of Criterion.<span></span>rs is to provide a powerful and statistically rigorous tool for measuring the performance of code, preventing performance regressions and accurately measuring optimizations. Additionally, it should be as programmer-friendly as possible and make it easy to create reliable, useful benchmarks, even for programmers without an advanced background in statistics.
102
103### Contributing
104
105First, thank you for contributing.
106
107One great way to contribute to Criterion.<span></span>rs is to use it for your own benchmarking needs and report your experiences, file and comment on issues, etc.
108
109Code or documentation improvements in the form of pull requests are also welcome. If you're not
110sure what to work on, try checking the
111[Beginner label](https://github.com/bheisler/criterion.rs/issues?q=is%3Aissue+is%3Aopen+label%3ABeginner).
112
113If your issues or pull requests have no response after a few days, feel free to ping me (@bheisler).
114
115For more details, see the [CONTRIBUTING.md file](https://github.com/bheisler/criterion.rs/blob/master/CONTRIBUTING.md).
116
117### Compatibility Policy
118
119Criterion.<span></span>rs supports the last three stable minor releases of Rust. At time of
120writing, this means Rust 1.50 or later. Older versions may work, but are not guaranteed.
121
122Currently, the oldest version of Rust believed to work is 1.46. Future versions of Criterion.<span></span>rs may
123break support for such old versions, and this will not be considered a breaking change. If you
124require Criterion.<span></span>rs to work on old versions of Rust, you will need to stick to a
125specific patch version of Criterion.<span></span>rs.
126
127### Maintenance
128
129Criterion.<span></span>rs was originally created by Jorge Aparicio (@japaric) and is currently being maintained by Brook Heisler (@bheisler).
130
131### License
132
133Criterion.<span></span>rs is dual licensed under the Apache 2.0 license and the MIT license.
134
135### Related Projects
136
137- [bencher](https://github.com/bluss/bencher) - A port of the libtest benchmark runner to stable Rust
138- [criterion](http://www.serpentine.com/criterion/) - The Haskell microbenchmarking library that inspired Criterion.<span></span>rs
139- [cargo-benchcmp](https://github.com/BurntSushi/cargo-benchcmp) - Cargo subcommand to compare the output of two libtest or bencher benchmark runs
140- [cargo-flamegraph](https://github.com/ferrous-systems/flamegraph) - Cargo subcommand to profile an executable and produce a flamegraph
141
142### Criterion.rs Extensions
143
144- [criterion-cycles-per-byte](https://crates.io/crates/criterion-cycles-per-byte) - A custom-measurement plugin that counts the number of CPU cycles used by the benchmark
145- [criterion-perf-events](https://crates.io/crates/criterion-perf-events) - A custom-measurement plugin that counts perf events created by the benchmark
146