-5

Hello guys and girls.

My question is simple one but I have no answer. Below you can see my C++ code - it is a little bit big but simple - when I run the code with //std::cout<<yy<<endl; (commented) it run less than 1 second, when I un-comment that line it runs about 2 minutes (I speak about Release because in Debug it run more time). Please pay attention that that line produce only few output lines (so the problem is not that the program have to call "cout" too many times) Thank you all. Here is my code:

#include <cstdlib>
#include <iostream>

using namespace std;

struct cell {
    int x;
    int y;
};

int main(int argc, char** argv) {
    cell vector[8];
    unsigned long int yy=0;

    for (int aaa = 0; aaa < 8; aaa++) {
        vector[0].x = aaa;
        for (int aab = 0; aab < 8; aab++) {
            vector[0].y = aab;
            for (int aac = 0; aac < 8; aac++) {
                vector[1].x = aac;
                for (int aad = 0; aad < 8; aad++) {
                    vector[1].y = aad;
                    for (int aae = 0; aae < 8; aae++) {
                        vector[2].x = aae;
                        for (int aaf = 0; aaf < 8; aaf++) {
                            vector[2].y = aaf;
                            for (int aag = 0; aag < 8; aag++) {
                                vector[3].x = aag;
                                for (int aah = 0; aah < 8; aah++) {
                                    vector[3].y = aah;
                                    for (int aai = 0; aai < 8; aai++) {
                                        vector[4].x = aai;
                                        for (int aaj = 0; aaj < 8; aaj++) {
                                            vector[4].y = aaj;
                                            for (int aak = 0; aak < 8; aak++) {
                                                vector[5].x = aak;
                                                for (int aal = 0; aal < 8; aal++) {
                                                    vector[5].y = aal;
                                                                    yy++;
                                                                    if(yy%10000000000==0){
                                                                    vector[5].y = aal-1;
                                                                    //std::cout<<yy<<endl;
                                                                    }

                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    std::cout<<yy<<endl;
    std::cout<<vector[5].y<<endl;
    return 0;
}
Sergiu Velescu
  • 472
  • 3
  • 9

1 Answers1

2

Most of this is a simple matter of optimization: as long as you don't produce any output from the loops, the compiler determines that the loops are basically dead code, and simply doesn't execute them at all. It has to do a little bit of pre-computation to determine the value that vector[5].y would have following the loops, but it can do that entirely at compile time, so at run-time, it's basically just printing out a fixed number.

When you produce visible output inside the loops, the compiler can't just eliminate executing them, so the code runs dramatically slower.

Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035
  • @LưuVĩnhPhúc: Looking more carefully, that was a red herring. – Jerry Coffin Sep 06 '17 at 16:14
  • Ok - I thought about this - what about "vector[5].y = aal-1;" in the loop - doesn't this force the compiler to run these loops and calculate the value? (at the end I COUT this value also) – Sergiu Velescu Sep 06 '17 at 16:16
  • It looks like the loops are running as when I comment one loop it takes 160ms to finish, if I un-comment it takes around 1 sec. – Sergiu Velescu Sep 06 '17 at 16:23
  • Looks like part of the loop is still there https://godbolt.org/g/WZDvrC . Perhaps the real story is that with all the cruft eliminated, the actual looping isn't too computationally expensive. – Mikhail Sep 06 '17 at 16:25
  • Some compilers may do this more aggressively than others. Clang removes all the loops, the code runs in .004s on my PC, gcc leaves some of them in and runs around .8s. Either way, the take-home message from this should be "Only profile meaningful code, and just putting stuff in a huge loop is not a proper microbenchmark." – Baum mit Augen Sep 06 '17 at 16:28