Speed of floating point types in C

I was curious about the influence on speed of the floating point types, so I've made a quick test. I leave the results here for reference.

In C there are three possible types to represent real numbers: float, double, long double in increasing order of precision and increasing size in memory (for a 64bit machine). To test there influence on speed I've made the following simple test: for each type, create a data structure representing a 3D vector using that type, calculate a bunch of dot product on instances of these structures, and measure and compare the execution time. The code is as follow:

(Edited on 2022/10/15 to add results on different architecture)

The results are as follow. For "11th Gen Intel(R) Core(TM) i3-1125G4 @ 2.00GHz" (from cat /proc/cpuinfo) and Row of chips LPDDR4 Synchronous 4267 MHz (from lshw -C memory):

Type-O0-O3
float0.0105760.001301
double0.0107470.002577
long double0.0352610.003683

For Intel Intel(R) Core(TM) i7-8700T CPU @ 2.40GHz and SODIMM DDR4 Synchronous 2400 MHz:

Type-O0-O3
float0.0085630.001006
double0.0088290.002198
long double0.0210980.002660

Without the optimisation turned on, there is no significative difference between float and double. With optimisation turned on float is around twice faster than double. In both case long double is significantly slower than the two other types.

2022-10-06
in All, C programming,
43 views
Copyright 2021-2024 Baillehache Pascal