Rust: f64 vs f32

Posted on

After my last post exploring SIMD instructions in Rust I was curious about the performance implications of f64 vs f32 values. My raytracer uses f64 values for all floating point calculations. If it was not clear from the last post my knowledge of low level CPU performance is limited at best. With that said I would not expect f32 to be any faster than f64 on modern 64-bit CPUs since you can do f64 calculations with a single CPU cycle anyway. In the benchmark mentioned in the previous post I tested the performance between f64 and f32 and found no difference.

I had some interesting discussion and feedback on the SIMD post that convinced me to try the f32 switch anyway. So I did and I then benchmarked the results.

Methodology

I rendered the above scene featuring the Stanford Bunny 10 times on my Macbook and Dekstop computer with both f64 floats and f32 floats. All tests were ran on the latest Rust nightly. My dekstop computer runs Windows 10 and my Macbook runs macOS Sierra.

Results

It turns out f32 is about 30% faster in both these cases presumably because of more efficient CPU cache usage and potentially because there’s more opportunity for effective pipelining in the CPU. For this project I do not expect I will need the precision offered by the double precision floats in f64 so I’ve decided to keep the f32 change.

The reason this simple scene is so slow on both computers is that I have only implemented very rudimentary acceleration structures.