0

So basically I would like to know, who is faster from these loops in C++ language? Basically, which of these examples would compile faster -

int S = 1, D = 2, d = 1;
for(int x = 0; x < 10000; x++) {
  S += D/2-d *s;
}

and

int S = 1, D = 2, d = 1, x = 0;
while(x < 10000) {
  x++;
  S += D/2-d * S;
}
Thats meeee
  • 55
  • 1
  • 1
  • 7
  • 3
    (1) "would compile faster"? do you mean "run faster?" (2) Let the compiler do the micro optimizations, you should do what is more readable. readability is for programmers and optimizations are for compilers – amit Feb 19 '12 at 16:41
  • possible duplicate of [What loop is faster, while or for](http://stackoverflow.com/questions/3629174/what-loop-is-faster-while-or-for) – Fred Larson Feb 19 '12 at 16:46
  • Does it really matter with the overhead of the loop? If it really does then a lot more must be provided. Which architecture are you running on, which compiler and which optimizations settings for the compiler? In theory the for loop could be removed altogether and the while could be replaced by x = 10000; – Roger Lindsjö Feb 19 '12 at 16:47
  • Well, basicaly, I men't run faster :), sorry about that. – Thats meeee Feb 19 '12 at 16:48
  • @DavidRodríguez-dribeas: Blimey.. my comment was only there for a handful of seconds! – Lightness Races in Orbit Feb 20 '12 at 11:46

5 Answers5

11

The second example would probably compile slightly faster because it is 76 characters long, as opposed to the first's 77 characters, and it uses less complicated language constructs (by less complicated, I mean less complicated to parse).

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
9

With any half-decent compiler it should compile to exactly the same code. In fact, this:

for (begin; cond; loop) {
    block;
}

Is semantically equivalent to (barring continue with the loop, as Seth Carnegie suggested below):

{
    begin;

    while (cond) {
         block;
         loop;
    }
}
orlp
  • 112,504
  • 36
  • 218
  • 315
  • 1
    Might not be **exactly** the same code, the `x++` in the for loop will be after changing `S`, won't it? – amit Feb 19 '12 at 16:44
  • 1
    @amit: Since `x++` is not used in the expression `S += D/2-d * S` and neither has side-effects the two expressions may be switched arbitrarily by the compiler. So neither order is defined :) – orlp Feb 19 '12 at 16:44
  • Those two are not exactly equivalent (things with `continue` and a few other things) – Seth Carnegie Feb 19 '12 at 16:45
  • @SethCarnegie: even then they are equivalent. – orlp Feb 19 '12 at 16:46
  • 2
    @nightcracker no, because if you `continue` in your `while` loop, the `loop` expression will not be run – Seth Carnegie Feb 19 '12 at 16:47
  • I think you mean "semantically equivalent". They're obvisouly not syntactically equivalent because they use different syntax. – CB Bailey Feb 19 '12 at 16:48
  • Additionally, the `while` example introduces two scopes whereas the `for` only introduces one – Seth Carnegie Feb 19 '12 at 16:49
  • Seth Carnegie: Not really, if you use C99: `for (int i=0; ...`. – orlp Feb 19 '12 at 16:50
  • What I mean is that you can't use variables from `block` in `loop`, like you could in the `while` example. But the answer is pretty correct so +1 :) – Seth Carnegie Feb 19 '12 at 16:56
2

There's a difference between compile time and execution time. For execution time, compilers nowadays will most likely generate the same code. Compilation time... most likely the same also.

Note that your example is incorrect. The two loops are not equivalent. The increment is executed first in the while loop. The equivalent of the for loop would be:

while(x < 10000) {
  S += D/2-d * S;
  x++;
}
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

The only 'person' able to answer that is a profiler.

On a more theoretical level, a compiler would take them down to the same low level constructs, if fact some compilers turn for loops into while loops before the generation of IR.

Necrolis
  • 25,836
  • 3
  • 63
  • 101
  • And it may even unroll the loops if it feels necessary! – BeRecursive Feb 19 '12 at 16:45
  • Or since the loop is determinate, it could just calculate the result and just initialise the variables to the values they would be at the end of the loop and remove the loop entirely. – Seth Carnegie Feb 19 '12 at 17:02
0

On my computer this code gives 4 ms (less than 0.1% ) of difference in average. And it doesn't matter which function goes first, a() or b(), the first is slower, so the reason is how scheduler works, I think.

#include <iostream>
#include  <QTime>

const long cycles = 1000000000;

void a()    {
    long s = 0;
    for(long x = 0; x < cycles; x++) {
        s++;
    }
}

void b()   {
    long x = 0;
    long s= 0;
    while(x < cycles) {
        x++;
        s++;
    }
}

int main(int argc, char *argv[])
{        
    QTime t1;
    t1.start();

    b();

    int i1 = t1.elapsed();
    t1.restart();

    a();

    int i2 = t1.elapsed();

    std::cout << i1 <<"\n"<< i2 <<"\n---\n";
    return 0;
}
Dmitriy Kachko
  • 2,804
  • 1
  • 19
  • 21