Avatar ·

Why does replacing 0.1f with 0 in a loop slow down the code by 10 times (C++, VS2010)?

Hi everyone! I'm a beginner in C++ and encountered a very strange issue while profiling code. I have a simple loop that calculates the sum of squares. Here's the code: ```cpp float sum = 0.0f; for (int i = 0; i < 100000000; ++i) { float x = i * 0.1f; // line A sum += x * x; } ``` This code runs very fast (about 0.2 seconds on my Visual Studio 2010 in Release x64). But if I replace `0.1f` with `0` (just zero): ```cpp float sum = 0.0f; for (int i = 0; i < 100000000; ++i) { float x = i * 0; // line B sum += x * x; } ``` ...then performance drops by 10 times! The code takes about 2 seconds to execute. I'm using Visual Studio 2010, compiling in Release mode (/O2), SSE2 enabled. Why is this happening? After all, multiplication by zero should be even faster than