0

I try to find out the exact execution time for "for loop" with 2e6 iteritions. The following code is ran within 10ms after compiled from g++ for c++ file. People told me that is optimization code automatically done by C++ compiler so you get meaningless execution time. In other words,since there is no any output call such as printf or cout<< for variable a,b,c so the optimized code will do nothing for that "for loop" that is why I got really short program execution time in 10ms. Right ? Why they said the time result is meaningless for "for loop".

Please advise

int main(){
int max = 2e6;
int a,b,c;
// CODE YOU WANT TO TIME
    int start = getMilliCount();
    for (int i = 0; i < max; i++) {
    a = 1234 + 5678 + i;
    b = 1234 * 5678 + i;
    c=1234/2+i;
   }
int milliSecondsElapsed = getMilliSpan(start);
printf("\n\nElapsed time = %u milliseconds %d\n", milliSecondsElapsed,max);
    return 0;
}
aabb
  • 113
  • 9
  • 2
    See [assembly output](http://goo.gl/HDSOiv). –  Sep 01 '14 at 11:17
  • 1
    The compiler can indeed remove completely the loop if it can prove it has no side-effects (like in your example). I don’t understand what you expect with `please advise`. Advise… what? – bolov Sep 01 '14 at 11:19
  • You could declare `c` volatile, to force its value to be calculated and assigned on each iteration. I don't know whether that result would be more or less meaningless. – Mike Seymour Sep 01 '14 at 12:02
  • http://www.network-theory.co.uk/docs/gccintro/gccintro_49.html this link has gcc optimzation options manual – aabb Sep 01 '14 at 12:36

2 Answers2

4

The run-time is absolutely not meaningless. It proves at least one important point: the optimizer is smarter than given credit, and it's able to deduce the loop has no side effects, so it cuts it out.

So even if the profile result only proves this one thing, it does have meaning.

To address what you want:

I try to find out the exact execution time for "for loop" with 2e8 iteritions.

The execution time of a for loop with 2e8 can be 0 if there are no observable effects. Or very large if they are. That's why you usually profile actual code using dedicated tools.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • So, actually, the for loop must be running for 2e8 iteritions on program exe file, Right ? So it is real time 10ms for the looping – aabb Sep 01 '14 at 11:19
  • @aabb I didn't say that. The for loop is probably not run at all. – Luchian Grigore Sep 01 '14 at 11:20
  • Since there is no other call or function in main() to call variable a,b,c. So if compiler is smart enough , the optimization code can be NOT including the code from "fop loop", Right ? if so, that is why I got 10ms that is meaningless for execution time for " for loop" please advise – aabb Sep 01 '14 at 11:22
  • @aabb The compiler can choose to optimize the loop out of the executable if it wants to. –  Sep 01 '14 at 11:23
  • I compile it with "gcc looptime.cpp -o looptime.exe" . how to compile it without any optimization process so I can check the time difference between optimization and non-optimization code ? – aabb Sep 01 '14 at 11:26
  • @aabb Compile with `-O0` – Baum mit Augen Sep 01 '14 at 11:27
  • @BaummitAugen profiling un-optimized code is useless and proves nothing. – Luchian Grigore Sep 01 '14 at 11:28
  • @aabb It may show him how great the optimizer is. :) – Baum mit Augen Sep 01 '14 at 11:29
  • yes it work for -O2 that optimize the code , and the speed is less than non-optimization code. The question is how I prove the code is not including "for loop"operation on optimization and non-optimization code ? Looking into both asm code will tell the answer whether the code is including for loop or not ? – aabb Sep 01 '14 at 11:56
2

The compiler can change the program in any way that does not change anything observable, i.e. all outputs etc. must be exactly the same as the outputs of the un-optimized code. In your example, the compiler may notice that the values of a, b and c after the loop are never used and the loop does nothing else, so it might as well remove the loop from your program.

It could also observe that the value of the variables depend directly on max and just skip all but the last iteration.

In both cases, the result would not depend on max. It still is not meaningless, it just means that you underestimate your compiler.

Edit:

I tested this scenario with g++ -O2, the loop gets completely removed and does not run at all.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
  • what is -O2 option ? How can I compile it without optimization so that I can see the time difference between optimization and NOT-optimization ? – aabb Sep 01 '14 at 11:30
  • http://stackoverflow.com/questions/668103/how-to-tell-compiler-to-not-optimize-certain-code-away The link is mentioned optimization – aabb Sep 01 '14 at 11:52
  • Sorry it is 2e6 not 2e8, and when I change to max from 2e6 to 2e8, the speed is 1000ms so (non-opt code: max=2e6->10ms and max=2e8-.1000ms) and (opt code: max=2e6->4ms and max=2e8->400ms) , but the result doesn't prove the "for loop" operation is executing or not – aabb Sep 01 '14 at 12:02
  • @aabb Cannot reproduce your result [with optimization.](http://coliru.stacked-crooked.com/a/c0017094895cf3ba) You can see that the loop is removed from the code by looking at the generated assembly (pass `-S` to gcc to see it). – Baum mit Augen Sep 01 '14 at 12:10
  • I tried this cpp looptime.cpp >looptime.i and g++ -S looptime.i and get looptime.s asm code , it shows as following where it prove there is no such "for loop" operation ? I can see there is asm code for "jmp" Could I attach asm code here ? – aabb Sep 01 '14 at 12:22
  • my output is compiled from non-optimization since I did not use -O2 on gcc , could I attach asm code here to see there is for loop operation or not ? – aabb Sep 01 '14 at 12:27
  • @aabb I cannot teach you asm in a comment on SO, go get a book if you want to learn it. I tested it, `-O2` removes the loop, `-O0` obviously does not. That should teach you to profile optimized code only to find bottlenecks. – Baum mit Augen Sep 01 '14 at 12:39
  • baum mit Augen,how you know it taking the for loop or not, you read it in its ASM code ? – aabb Sep 01 '14 at 12:43
  • what else I need to do , or I just click the answer to green color and that is all. And I don't need to do anything, right ? Why there is icon of "answer your quetion" at the bottom that is grading the answer by asker or something else ? – aabb Sep 01 '14 at 13:13