Why does changing 0.1f to 0 slow down performance by 10x?

Why does changing 0.1f to 0 slow down performance by 10x?
Hey there tech enthusiasts! 👋 Have you ever come across a situation where a tiny change in code causes a significant slowdown in performance? 🚀 Well, today we're going to dive into a intriguing question that might pique your curiosity: Why does changing 0.1f to 0 slow down performance by 10x? 🤔
The Context
Let's take a look at this code snippet:
const float x[16] = { 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8,
1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6};
const float z[16] = {1.123, 1.234, 1.345, 156.467, 1.578, 1.689, 1.790, 1.812,
1.923, 2.034, 2.145, 2.256, 2.367, 2.478, 2.589, 2.690};
float y[16];
for (int i = 0; i < 16; i++)
{
y[i] = x[i];
}
for (int j = 0; j < 9000000; j++)
{
for (int i = 0; i < 16; i++)
{
y[i] *= x[i];
y[i] /= z[i];
y[i] = y[i] + 0.1f; // <--
y[i] = y[i] - 0.1f; // <--
}
}And now compare it to this nearly identical code snippet, except for one small change:
const float x[16] = { 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8,
1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6};
const float z[16] = {1.123, 1.234, 1.345, 156.467, 1.578, 1.689, 1.790, 1.812,
1.923, 2.034, 2.145, 2.256, 2.367, 2.478, 2.589, 2.690};
float y[16];
for (int i = 0; i < 16; i++)
{
y[i] = x[i];
}
for (int j = 0; j < 9000000; j++)
{
for (int i = 0; i < 16; i++)
{
y[i] *= x[i];
y[i] /= z[i];
y[i] = y[i] + 0; // <--
y[i] = y[i] - 0; // <--
}
}When compiling with Visual Studio 2010 SP1, using the optimization level -O2 and sse2 enabled, you may be surprised to find that the first snippet runs more than 10 times faster than the second one! 😮
The Explanation
Now, let's break down why such a minute change from 0.1f to 0 slows down performance so drastically. 💨
The reason lies in how floating-point numbers are represented in the computer's memory. Floating-point numbers, unlike integers, don't have an exact representation due to constraints in memory and hardware. This approximation introduces a certain degree of imprecision. 😶
When dealing with floating-point numbers, a common practice is to perform calculations with a certain tolerance. For example, instead of comparing x and y directly (if (x == y)), it's more appropriate to check if the difference between x and y is within an acceptable tolerance (if (abs(x-y) < tolerance)).
In our original code snippet, when we add 0.1f to each element in the y array, it triggers an additional floating-point computation. This computation takes time since it involves the representation and approximation of the floating-point number 0.1f. Each addition and subtraction operation with 0.1f in the loop creates a cumulative slowdown.
However, in the modified code snippet, where we add and subtract 0 instead of 0.1f, the compiler recognizes that adding or subtracting 0 has no effect on the value. Therefore, it optimizes this computation away, resulting in improved performance.
The Solution
If you find yourself in a situation where small changes in floating-point calculations significantly impact performance, there are a few strategies you can employ:
Minimize unnecessary operations: Avoid unnecessary operations that have no impact on the result, such as adding or subtracting zero values.
Use tolerance-based comparisons: Instead of direct equality comparisons between floating-point values, use tolerance-based comparisons to account for the imprecision in the representation of floating-point numbers.
Consider alternative data types: Depending on your specific use case, alternative data types such as fixed-point arithmetic or specialized libraries for numerical computations can provide more precise results while maintaining performance.
Wrap Up and Engage!
So, there you have it! The seemingly innocent change from 0.1f to 0 in our code snippet that resulted in a significant performance slowdown. Understanding how floating-point numbers are handled by the computer and being mindful of unnecessary operations can help you optimize your code and avoid similar pitfalls.
If you've encountered similar situations or have any questions, let's discuss it! Share your experiences in the comments below and let's geek out together! 🤓💬
Remember, stay curious, keep coding, and happy optimizing! 🚀✨
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.



