Crates.io | iai |
lib.rs | iai |
version | 0.1.1 |
source | src |
created_at | 2021-01-24 15:45:04.67778 |
updated_at | 2021-01-24 15:48:17.577031 |
description | One-shot benchmarking library |
homepage | |
repository | https://github.com/bheisler/iai |
max_upload_size | |
id | 346070 |
size | 38,858 |
Iai is an experimental benchmarking harness that uses Cachegrind to perform extremely precise single-shot measurements of Rust code.
In order to use Iai, you must have Valgrind installed. This means that Iai cannot be used on platforms that are not supported by Valgrind.
To start with Iai, add the following to your Cargo.toml
file:
[dev-dependencies]
iai = "0.1"
[[bench]]
name = "my_benchmark"
harness = false
Next, define a benchmark by creating a file at $PROJECT/benches/my_benchmark.rs
with the following contents:
use iai::{black_box, main};
fn fibonacci(n: u64) -> u64 {
match n {
0 => 1,
1 => 1,
n => fibonacci(n-1) + fibonacci(n-2),
}
}
fn iai_benchmark_short() -> u64 {
fibonacci(black_box(10))
}
fn iai_benchmark_long() -> u64 {
fibonacci(black_box(30));
}
iai::main!(iai_benchmark_short, iai_benchmark_long);
Finally, run this benchmark with cargo bench
. You should see output similar to the following:
Running target/release/deps/test_regular_bench-8b173c29ce041afa
bench_fibonacci_short
Instructions: 1735
L1 Accesses: 2364
L2 Accesses: 1
RAM Accesses: 1
Estimated Cycles: 2404
bench_fibonacci_long
Instructions: 26214735
L1 Accesses: 35638623
L2 Accesses: 2
RAM Accesses: 1
Estimated Cycles: 35638668
The primary goal of Iai is to provide a simple and precise tool for reliably detecting very small changes to the performance of code. Additionally, it should be as programmer-friendly as possible and make it easy to create reliable, useful benchmarks.
I intend Iai to be a complement to Criterion-rs, not a competitor. The two projects measure different things in different ways and have different pros, cons, and limitations, so for most projects the best approach is to use both.
Here's an overview of the important differences:
For benchmarks that run in CI (especially if you're checking for performance regressions in pull requests on cloud CI) you should use Iai. For benchmarking on Windows or other platforms that Valgrind doesn't support, you should use Criterion-rs. For other cases, I would advise using both. Iai gives more precision and scales better to larger benchmarks, while Criterion-rs allows for excluding setup time and gives you more information about the actual time your code takes and how strongly that is affected by non-determinism like threading or hash-table randomization. If you absolutely need to pick one or the other though, Iai is probably the one to go with.
First, thank you for contributing.
One great way to contribute to Iai is to use it for your own benchmarking needs and report your experiences, file and comment on issues, etc.
Code or documentation improvements in the form of pull requests are also welcome. If you're not sure what to work on, try checking the Beginner label.
If your issues or pull requests have no response after a few days, feel free to ping me (@bheisler).
For more details, see the CONTRIBUTING.md file.
Iai supports the last three stable minor releases of Rust. At time of writing, this means Rust 1.48 or later. Older versions may work, but are not tested or guaranteed.
Currently, the oldest version of Rust believed to work is 1.48. Future versions of Iai may break support for versions older than 3-versions-ago, and this will not be considered a breaking change. If you require Iai to work on old versions of Rust, you will need to stick to a specific patch version of Iai.
Iai was originally written and is maintained by Brook Heisler (@bheisler)
Iai is dual licensed under the Apache 2.0 license and the MIT license.