# Branchless Rust: benchmarks Companion benchmarks for the article [Branchless Rust](https://www.greyblake.com/blog/branchless-rust/). The task: filter a slice of one million random `f64` values, keeping the elements greater than a given threshold. There are three implementations in [`src/lib.rs`](src/lib.rs): * `filter_iter` - the idiomatic iterator version * `filter_prealloc` - a loop with a preallocated output vector * `filter_branchless` - the branchless version (always write, conditionally advance) The benchmarks in [`benches/filter.rs`](benches/filter.rs) run every implementation at different selectivities (1%, 25%, 50%, 75%, 99% of elements kept), plus one extra experiment: the idiomatic version on the same data sorted and shuffled. ## How to run ``` cargo bench ``` The whole run takes a few minutes. Criterion prints the results to the terminal and generates an HTML report at `target/criterion/report/index.html`. ## Results Measured on Intel Core i7-10875H, Rust 1.96.0: | kept | `filter_iter` | `filter_prealloc` | `filter_branchless` | |-----:|--------------:|------------------:|--------------------:| | 1% | 0.59 ms | 0.68 ms | 1.09 ms | | 25% | 2.69 ms | 2.39 ms | 1.05 ms | | 50% | 3.94 ms | 3.87 ms | 1.03 ms | | 75% | 2.75 ms | 2.72 ms | 1.02 ms | | 99% | 1.49 ms | 1.23 ms | 1.11 ms | Sorted vs shuffled input (`filter_iter`, 50% kept): | input | time | |----------|--------:| | shuffled | 4.15 ms | | sorted | 0.93 ms | ## License MIT