c++ - Will the compiler optimize escaping an inner loop? -
I have such code (all the uses shown):
bool = false ; For (int i = 0; i & lt; large; i ++) {... for (int j = 0; j & lt; wah; j ++) {... if (foo (i, J)) {done = true; break; } ...} if (done) break; ...}
Any compiler will convert it to:
for (int i = 0; i
Note: When I'm most interested in the break (if done);
is removed as a bypass and dead code, I am also interested in that if more done
is removed completely.
Obviously it depends on the compiler. When you are unsure, the best thing to do is to look at the compiler's assembly output (all popular compilers are a switch for this). Even if you are not familiar with the assembly, you can compare the debug version with the least customized version.
It is being said, This is one of the few situations where goto
is not a bad idea to use it to break the internal loops Feel free to
Edit
The following attempts have been made in VS2010 and it actually optimizes external conditional:
Bool done = false; For (int i = 0; i & lt; 10; i ++) {for (int j = 0; j & lt; 10; j ++) {if (i == 7 and j == 3 ) {Done = true; break; }} If (done) break; } Return 0;
Comments
Post a Comment