This repository contains a benchmark between different implementations of the Fast Inverse Square Root in Rust. The methods compared are using std::mem::compute
and pointer casts. An implementation using f64::sqrt
is also shown for reference.
This uses benchmark tests which are at the time of writing not stable so to run them you need a nightly build of rust. Installing and using nightly builds is covered in the rustup docs;
With a nightly rust activated run the following
cargo bench
By default this will calculate 100 million inverse square roots per iteration. This value can be overridden with the environment variable NUM_ITERATIONS
.
NUM_ITERATIONS=100 cargo bench # Using 100 calculations per iteration
Using rustc 1.17.0-nightly (49c67bd63 2017-03-24)
running 3 tests
test tests::bench_isqrt_no_transmute ... bench: 282,948,645 ns/iter (+/- 49,056,853)
test tests::bench_isqrt_sqrt ... bench: 965,164,677 ns/iter (+/- 20,426,067)
test tests::bench_isqrt_transmute ... bench: 273,885,037 ns/iter (+/- 15,464,136)
test result: ok. 0 passed; 0 failed; 0 ignored; 3 measured
running 3 tests
test tests::bench_isqrt_no_transmute ... bench: 28,101,823 ns/iter (+/- 3,276,136)
test tests::bench_isqrt_sqrt ... bench: 96,460,162 ns/iter (+/- 2,735,941)
test tests::bench_isqrt_transmute ... bench: 27,624,626 ns/iter (+/- 2,690,027)
test result: ok. 0 passed; 0 failed; 0 ignored; 3 measured