6

Possible Duplicate:
while (1) Vs. for (;;) Is there a speed difference?

Hi,

Which is better,faster and more optimized way to implement infinite loop - for(;;) or while(1)? and why?

Community
  • 1
  • 1
iSegFault
  • 947
  • 2
  • 9
  • 18

6 Answers6

50

I prefer for(;;) because it doesn't test anything and semantically this is what you mean. It doesn't make so much sense to keep testing if 1 is true. However any professional C programmer should immediately recognize both idioms as both are used.

In terms of actual performance there should be no difference. The compiler will optimize away the test.

I tried testing both to see which is faster but neither of them has completed yet.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
19

In any normal compiler, there should be absolutely no difference. For example, here's what LLVM-clang generates (with the -O3 flag) for while (1) {}:

    .file   "test.c"
    .text
    .globl  main
    .align  16, 0x90
    .type   main,@function
main:
    pushl   %ebp
    movl    %esp, %ebp
    .align  16, 0x90
.LBB0_1:
    jmp .LBB0_1

Note the jmp .LBB0_1 part, which is the actual infinite loop. For the for (;;) kind, it generates absolutely the same code.

You can also try with other compilers for fun, but it's best just stop worrying about it.


OK, I just had to try with gcc as well:

    .file   "test.c"
    .text
.globl main
    .type   main, @function
main:
    pushl   %ebp
    movl    %esp, %ebp
.L2:
    jmp .L2
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
4

I would argue neither is more optimized, as neither will accomplish the task of looping infinitely in any measurable amount of time.

Is it really possible to reach infinity more or less efficiently?

kbrimington
  • 25,142
  • 5
  • 62
  • 74
  • 8
    True fact: Jon Skeet can execute an infinite loop in three and a half seconds flat. – Michael Myers Aug 17 '10 at 18:41
  • @mmyers: I thought that was Chuck Norris? – FrustratedWithFormsDesigner Aug 17 '10 at 18:45
  • 1
    ...and by removing a single line of code he can optimize it to run in 0 seconds. – Will A Aug 17 '10 at 18:46
  • 1
    @Frustrated: Jon Skeet is the new Chuck Norris – Eli Bendersky Aug 17 '10 at 18:47
  • 1
    @Frustrated: http://meta.stackexchange.com/questions/9134/jon-skeet-facts/9199#9199 – Michael Myers Aug 17 '10 at 19:53
  • 1
    It's not possible actually to reach infinity more or less efficiently, but it is possible to go through the motions at a faster or slower rate; 2^n never reaches infinity in the same way that log(n) doesn't either, but my goodness, log(n) will take you a whole whack longer! (Sure, they both tend to infinity as n tends to infinity, but this is where the whole field of analysis comes in; exponents beat powers beat logs etc.) – Savara May 05 '13 at 13:09
2

In theory, a completely naive compiler could store the literal '1' in the binary (wasting space) and check to see if 1 == 0 every iteration (wasting time and more space).

In reality, however, even with "no" optimizations, compilers will still reduce both to the same. They may also emit warnings because it could indicate a logical error. For instance, the argument of while could be defined somewhere else and you not realize it's constant.

Nick T
  • 25,754
  • 12
  • 83
  • 121
1

Same thing. compiler will translate it to a single JMP instruction.

Lior Kogan
  • 19,919
  • 6
  • 53
  • 85
0

This is my opinion (no research done):

When debugging code as I step through it, for for loops it makes me go to all three parts the first time, then two the rest of the iterations. However, the while loop only has one. Just a thought.

XstreamINsanity
  • 4,176
  • 10
  • 46
  • 59