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

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for 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:

  1. Minimize unnecessary operations: Avoid unnecessary operations that have no impact on the result, such as adding or subtracting zero values.

  2. 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.

  3. 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.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my