Why does this bit of code,

```
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; // <--
}
}
```

run more than 10 times faster than the following bit (identical except where noted)?

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

The optimization level was `-02`

with `sse2`

enabled.

I haven't tested with other compilers.

## Best Answer

Welcome to the world of denormalized floating-point!They can wreak havoc on performance!!!Denormal (or subnormal) numbers are kind of a hack to get some extra values very close to zero out of the floating point representation. Operations on denormalized floating-point can be

than on normalized floating-point. This is because many processors can't handle them directly and must trap and resolve them using microcode.tens to hundreds of times slowerIf you print out the numbers after 10,000 iterations, you will see that they have converged to different values depending on whether

`0`

or`0.1`

is used.Here's the test code compiled on x64:

Output:Note how in the second run the numbers are very close to zero.

Denormalized numbers are generally rare and thus most processors don't try to handle them efficiently.

To demonstrate that this has everything to do with denormalized numbers, if we

flush denormals to zeroby adding this to the start of the code:Then the version with

`0`

is no longer 10x slower and actually becomes faster. (This requires that the code be compiled with SSE enabled.)This means that rather than using these weird lower precision almost-zero values, we just round to zero instead.

Timings: Core i7 920 @ 3.5 GHz:In the end, this really has nothing to do with whether it's an integer or floating-point. The

`0`

or`0.1f`

is converted/stored into a register outside of both loops. So that has no effect on performance.