Will the compiler optimize escaping an inner loop?

Posted by BCS on Stack Overflow See other posts from Stack Overflow or by BCS
Published on 2010-06-16T01:18:47Z Indexed on 2010/06/16 1:22 UTC
Read the original article Hit count: 230

Filed under:
|
|
|

The code I have looks like this (all uses of done shown):

bool done = false;
for(int i = 0; i < big; i++)
{
  ...
  for(int j = 0; j < wow; j++)
  {
    ...
    if(foo(i,j))
    {
       done = true;
       break;
    }
    ...
  }
  if(done) break;
  ...
}

will any compilers convert it to this:

for(int i = 0; i < big; i++)
{
  ...
  for(int j = 0; j < wow; j++)
  {
    ...
    if(foo(i,j))
      goto __done; // same as a labeled break if we had it
    ...
  }
  ...
}
__done:;

© Stack Overflow or respective owner

Related posts about c++

Related posts about optimization