Intel C ++ compiler understands which optimization is done

I have a code segment that is as simple as:

for( int i = 0; i < n; ++i)
{
  if( data[i] > c && data[i] < r )
  {
    --data[i];
  }
}

This is part of a great function and project. It actually rewrites another cycle that turned out to be time consuming (long loops), but I was surprised by two things:

When the data [i] was temporarily stored as follows:

for( int i = 0; i < n; ++i)
{
  const int tmp = data[i];
  if( tmp > c && tmp < r )
  {
    --data[i];
  }
}

It got a lot slower. I am not saying that it should be faster, but I cannot understand why it should be much slower, the compiler should be able to figure out whether to use tmp or not.

But more importantly, when I moved the code segment to a separate function, it became about four times slower. I wanted to understand what was happening, so I looked at the option report and in both cases the loop is vectorized and seems to do the same optimization.

, , ​​ , , ? ?

, , .

:

, - , , . , tmp, , .

+5
3

, , . , x86 , .. . , , . aliasing , , , , - , .

, , , , , ( push/pop/etc).

: , . , . , Intel, , , .

: , , . . , , , , , .

. , , - , . , V++ , . Intel, , .

V++ - , . , . std::for_each .

+4

, / . , .

, . , , , , , .

, , Intel. , .

+1

,

for( int i = 0; i < n; ++i)
{
  const int tmp = data[i]; //?? declaration inside a loop
  if( tmp > c && tmp < r )
  {
    --data[i];
  }
}

. , .

for( int tmp, i = 0; i < n; ++i)
{
  tmp = data[i];
  if( tmp > c && tmp < r )
  {
    --data[i];
  }
}

. size_t (uint) . int - , -.

int tmp; // well if you must have your temporary, I don't see why you want it,
         // it costs you 1 register although that should not matter much here.
for( size_t i = 0; i < n; ++i)
{
  tmp = data[i];
  if( tmp > c && tmp < r )
  {
    --data[i];
  }
}

.

-2
source

All Articles