C / C ++: GOTO is faster than WHILE and FOR?

I know that everyone hates GOTO and no one recommends it. But that is not the point. I just want to know which code is the fastest:

  • goto loop

     int i=3; loop: printf("something"); if(--i) goto loop; 
  • while

     int i=3; while(i--) { printf("something"); } 
  • for loop

     for(int i=3; i; i--) { printf("something"); } 
+7
source share
8 answers

Generally speaking, for and while loops compile with the same thing as goto , so this usually will not change the situation. If you have doubts, you can try all three and see which takes longer. Most likely, you will not be able to measure the difference, even if you loop a billion times.

If you look at this answer , you will see that the compiler can generate exactly the same code for for , while and goto (only in this case there were no conditions).

+12
source

Write short programs, then do the following:

 gcc -S -O2 p1.c gcc -S -O2 p2.c gcc -S -O2 p3.c 

Analyze the output and see if there is a difference. Remember to introduce some level of unpredictability so that the compiler does not optimize the program to zero.

Compilers do a great job of optimizing these trivial issues. I would advise you not to worry about it and instead focus on what makes you more productive as a programmer.

Speed ​​and efficiency is a wonderful thing to worry about, but in 99% of cases it involves using the right data structures and algorithms ... without worrying about whether for faster than while or goto , etc.

+6
source

The only time I saw an argument made for goto was in one of W. Richard Stevens' articles or books. His point was that in a very time-critical section of code (I believe that his example was the network stack), nested if / else blocks with the appropriate error handling code can be redone using goto in such a way that it has valuable value.

Personally, I'm not a good programmer to argue with Stevens' work, so I will not try. goto may be useful for performance issues, but the limits of when it is are pretty strict.

+4
source

This is probably a compiler, and an optimizer, and architecture.

For example, the code if(--i) goto loop; is a conditional test followed by an unconditional branch. The compiler can simply generate the appropriate code, or it can be smart enough (although a compiler that did not have at least so many skills may not cost much) to create a single conditional branch instruction. while(i--) , on the other hand, is already a conditional branch at the source level, so switching to a conditional branch at the machine level can be stronger, regardless of the complexity of the implementation or the optimizer of the compiler.

In the end, the difference is like a minute and only matters if a large number of iterations are required, and how you should answer this question is to build code for a specific purpose and compiler (and compiler parameters) of interest and either check the result machine level code, or directly measure the runtime.

In your examples, printf () in the loop will dominate at any time anyway; something simpler in the loop would make it easier to observe the differences. I would suggest an empty loop and then declare i volatile to prevent loop optimization.

+3
source

As long as you generate the same control flow as a regular loop, almost any decent compiler can and will generate the same code if you use for , while , etc.

You can get something from using goto , but usually only if you generate a control flow that a normal loop just cannot (at least cleanly). A typical example is to jump to the middle of the loop to get the construction of the loop and half, which in most language expression statements (including C) does not provide cleanliness.

+2
source

There should be no significant difference between all loops and goto. Besides the idea, this compiler will most likely not try to optimize GOTO things at all.

And it makes no sense to try to optimize the things the compiler creates in loops. It is more advisable to optimize the code inside the loop or reduce the number of iterations or so on.

+2
source

I think that after the compiler, the code after nornal will be installed.

In fact, I think goto is very convenient sometimes, although it's hard to read.

+1
source

There are several niche verticals in which goto is still commonly used as standard practice, some very smart people, and there is no prejudice against goto in these settings. I used to work for a simulation-oriented company, where all the local fortran codes had gotos, the team was super smart, and the software worked almost flawlessly.

So, we can leave the dignity of goto aside, and if the question is only to compare the loops, then we do this by profiling and / or comparing the assembly code. However, the question includes statements like printf, etc. However, you cannot discuss the optimization of loop control logic. In addition, as others have indicated, these loops will generate VERY similar machine codes.

All conditional branches are considered β€œtaken” (true) in the pipeline architectures of the processors one way or another prior to the decoding phase, in addition to small loops that usually expand to be inconclusive. Thus, in accordance with the Harper point above, it is unrealistic that goto has any advantage in simple contour control (just as at the same time it does not have an advantage over each other). GOTO usually makes sense in multiple nested loops or multiple nested ifs when adding an additional condition checked by goto in EACH from nested loops or nested ifs is suboptimal.

When optimizing the look of a search in a simple cycle, using a donor is sometimes more efficient than anything else. In fact, by adding a dummy value to the end of the array, you can avoid checking two conditions (the end of the array and the found value) with only one condition (the value is found) and which preserves the internal CMP operations. I do not know if compilers do this automatically.

0
source

All Articles