-2

Let's assume C++ for loop in the form of:

for(int i; i<10;i++)

So the integer needs to be allocated in the beginning and then increased at every step the as well as compared. So wouldn't it be faster to do something like that:

f or(int i; i++<10;)

since the variable doesn't need to be loaded into the storage again? espacially when making it volatile?

This little example code gave the same result for all the cases. Does the for loop get optimized anyway or am I missing something?

#include<iostream>
#include<ctime>

int main() {
    time_t start,ende;
    volatile int dummy = 1;
    const int rep = 1000000000;

    // Method 1
    start = time(0);
    for (int i = 0; i < rep; i++)
            dummy = 1;

    ende = time(0);                         
    std::cout << "Method 1: " << difftime(ende,start)*1000 << " ms" << std::endl;

    // Method 2
    start = time(0);
    for (int i = 0; i++ < rep; )
            dummy = 1;                                                              
    ende = time(0);
    std::cout << "Method 2: " << difftime(ende,start)*1000 << " ms" << std::endl;

    // Method 3
    start = time(0);
    for (volatile int i = 0; i < rep; i++)
            dummy = 1;

    ende = time(0);                         
    std::cout << "Method 3: " << difftime(ende,start)*1000 << " ms" << std::endl;
}

OS: Linux Compiler: g++ Optimization: standart (no flag)

magu_
  • 4,766
  • 3
  • 45
  • 79
  • 2
    Profiling with no optimization is moot, turn on optimizations and draw conclusions from that. – Luchian Grigore Apr 22 '14 at 11:39
  • 1
    why do you use `volatile`? – Karoly Horvath Apr 22 '14 at 11:41
  • @LuchianGrigore. I did the run also with O3 without any change. I didn't want to draw a conclusion too fast, thats why I asked here – magu_ Apr 22 '14 at 11:41
  • @KarolyHorvath. The idea was to make sure that it will indeed run trough the for loops and not simply detect that nothing changes and therefore simply shortcut it. – magu_ Apr 22 '14 at 11:43
  • The compiler may well optimize those loops away. – David Hammen Apr 22 '14 at 11:43
  • 1) C++ is a high-level programming language, don't make weird assumptions about the generated code 2) when in doubt, *check* the generated code and measure. checking the code is a lot easier and safer than using `volatile` (which will destory a lot of optimization opportunities and will give a false bias to certain things). 3) in case the whole loop gets optimized away, do some non-trivial math summation and use the result... – Karoly Horvath Apr 22 '14 at 11:44
  • @KarolyHorvath. I agree that I should check the results, that's actually what I try to do here. Since I'm not familiar with assembler I cannot really check the generated code. – magu_ Apr 22 '14 at 11:49
  • @magu_: 1) it's easier than you think. give it a try. 2) even if you don't understand it, you can still compare whether it generated the *same* code. – Karoly Horvath Apr 22 '14 at 11:51
  • `time` only measures whole seconds, so you might not see small differences at all. – Mike Seymour Apr 22 '14 at 12:06

2 Answers2

1

Compilers are a lot smarter than you think. They won't produce the kind of spurious load that you think they do.

MSalters
  • 173,980
  • 10
  • 155
  • 350
1

Optimization depends on compiler and its options.

I'd suggest you to disassemble your code and see what's the result of the optimization. A simple good disassemble e.g. HT editor or IDA version 5 (it free now). For small piece of code it will be easy enough.

mblw
  • 1,762
  • 1
  • 19
  • 28
  • Thank you for your answer. Unfortunatly I do not know any assembler therefore I'm a bit lost. – magu_ Apr 22 '14 at 11:49
  • For a small code snippet it's really not hard as could seemed. But if your really interesting in compiler optimization and it make sense for your, knowing of assembler is very useful and could help to estimate your code performance. Your variable permutation does make nothing for code performance because compiler analyze it and generate optimized result where `rep` and `i` may not be presented at all as separated variables. – mblw Apr 22 '14 at 14:39
  • True I should look into it at some point. – magu_ Apr 23 '14 at 05:46