While curious on how C code are optimized by compiler on mathematical functions, two implementations of the standard normal distribution are compared in terms of performance. The aim is to provide insight on how the generated machine code performs, without having to actually inspecting them. The function is an approximation function coded in standard C. It should be noted that both functions implement the same approximation, but the actual equation is a little bit different in terms of number of multiplication operator. The first implementation is with less multiplication operators:
While the second one is a modified version with more multiplication operators, e.g. expanding
A scaffolding test rig is used to loop 10 million times, within it, the approximation function is called from 0 to 1 in steps of 0.1. Visual Studio is used for the code compilation, and 20 samples are collected from each of the two functions. For the analysis, the TI-84 Pocket SE is used to carry out 2-sample T test in the procedure below:
By evaluating the p-value, It looks like the first version of more compact C code performed better and perhaps there is little the compiler can help here.