https://github.com/yoav-lavi/benchmark-scratchpad
A quick scratchpad for benchmarking Rust code
https://github.com/yoav-lavi/benchmark-scratchpad
benchmark rust rust-lang scratchpad
Last synced: over 1 year ago
JSON representation
A quick scratchpad for benchmarking Rust code
- Host: GitHub
- URL: https://github.com/yoav-lavi/benchmark-scratchpad
- Owner: yoav-lavi
- License: mit
- Created: 2022-03-28T18:30:18.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-03-29T18:07:41.000Z (over 4 years ago)
- Last Synced: 2025-01-24T14:27:05.478Z (over 1 year ago)
- Topics: benchmark, rust, rust-lang, scratchpad
- Language: Rust
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Benchmark Scratchpad
A quick scratchpad for benchmarking Rust code
## Prerequisites
- [cargo-make](https://github.com/sagiegurari/cargo-make) - `cargo install --force cargo-make`
## Instructions
1. Edit [benches/benchmark.rs](benches/benchmark.rs)
2. Run `cargo make bench`
## `benchmarks!`
The `benchmarks!` macro accepts input with the following structure:
```rust
benchmarks! {
benchmark_name { /* benchmark impl */ }
other_benchmark_name { /* other benchmark impl */ }
}
```
The [criterion.rs](https://github.com/bheisler/criterion.rs) `black_box` function is included and usable within the macro.
`criterion::black_box` stops the compiler from constant-folding away the whole function and replacing it with a constant.
You can you the keyword `@skip` before a benchmark for it not to be run:
```rust
benchmarks! {
@skip benchmark_name { /* benchmark impl */ }
}
```
## Tasks
- `cargo make bench` - runs the benchmark via `cargo-criterion`
- `cargo make bench-verbose` - runs the benchmark via `cargo-criterion` with verbose output
- `cargo make reset` - resets the criterion data for earlier benchmarks
- `cargo make new` - returns [benches/benchmark.rs](benches/benchmark.rs) to the initial state
- `cargo make save [FILENAME]` - saves the contents of [benches/benchmark.rs](benches/benchmark.rs) to `saved/[FILENAME].rs`
## Example
```rust
mod internals;
benchmarks! {
collect_vec_string {
let vector = black_box(vec!["hello", "world"]);
let _output = vector
.iter()
.map(|item| item.to_uppercase())
.collect::>()
.join("");
}
collect_string {
let vector = black_box(vec!["hello", "world"]);
let _output = vector
.iter()
.map(|item| item.to_uppercase())
.collect::();
}
}
```
## Uses
- [cargo-make](https://github.com/sagiegurari/cargo-make)
- [cargo-criterion](https://github.com/bheisler/cargo-criterion)
- [criterion.rs](https://github.com/bheisler/criterion.rs)