Winston Posted November 2, 2005 Share Posted November 2, 2005 As the title describes it all, i don't actually get why many people prefer for(;;) i've heard it's more efficient, but how does that work? Link to comment https://www.neowin.net/forum/topic/392657-loop-efficiency-comparison-whiletrue-vs-for/ Share on other sites More sharing options...
0 Ianmac45 Posted November 2, 2005 Share Posted November 2, 2005 it's the same in my experiences....... the only thing i can think of is that the computer doesn't have to do any work when going to the next cycle for the for(;;) loop. with the while(true) loop, in each cycle the computer has to check to see if true is ... well, true i hope that helps with this oh-so-complicated question/task Link to comment https://www.neowin.net/forum/topic/392657-loop-efficiency-comparison-whiletrue-vs-for/#findComment-586755655 Share on other sites More sharing options...
0 smurfiness Posted November 2, 2005 Share Posted November 2, 2005 Quote this oh-so-complicated question/task *LOL* Personally, I think it's just that they want to look smarter. while(true) looks like a newbie mistake. for(;;) looks like you know what you're doing. I doubt either is any more efficient than the other. If the for loop actually is more efficient, the difference would be measured in thousandths (if even that much) of a second. Link to comment https://www.neowin.net/forum/topic/392657-loop-efficiency-comparison-whiletrue-vs-for/#findComment-586756732 Share on other sites More sharing options...
0 kjordan2001 Posted November 2, 2005 Share Posted November 2, 2005 Well, at least in C using gcc as the compiler, there is no difference. The assembly code output is identical. While loop: .file "infwhile.c" .section .rodata .LC0: .string "Hello world\n" .text .globl main .type main, @function main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp movl $0, %eax addl $15, %eax addl $15, %eax shrl $4, %eax sall $4, %eax subl %eax, %esp .L2: movl $.LC0, (%esp) call printf jmp .L2 .size main, .-main .section .note.GNU-stack,"",@progbitsident "GCC: (GNU) 3.4.4 (Gentoo 3.4.4-r1, ssp-3.4.4-1.0, pie-8.7.8)" For loop: .file "inffor.c" .section .rodata .LC0: .string "Hello world\n" .text .globl main .type main, @function main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp movl $0, %eax addl $15, %eax addl $15, %eax shrl $4, %eax sall $4, %eax subl %eax, %esp .L2: movl $.LC0, (%esp) call printf jmp .L2 .size main, .-main .section .note.GNU-stack,"",@progbitsident "GCC: (GNU) 3.4.4 (Gentoo 3.4.4-r1, ssp-3.4.4-1.0, pie-8.7.8)" And the code to produce those was just for(;;) { printf("Hello world\n"); } while (1) { printf("Hello world\n"); } How this works out in other languages, I can't be sure. It also depends on how the compiler interprets it. But an infinite loop is just calling the same block of code over and over. So that means once you reach the end of the code, you jump back up to the start of it, which is what we see here. There is no real difference between a for loop and a while loop in assembly, although they can differ some depending on how the compiler handles it. I've heard some people state that while loops are faster, but whether or not that's true, I don't know. It shouldn't be that way, but depending on the code and the compiler, it may be true. Link to comment https://www.neowin.net/forum/topic/392657-loop-efficiency-comparison-whiletrue-vs-for/#findComment-586756800 Share on other sites More sharing options...
Question
Winston
As the title describes it all, i don't actually get why many people prefer for(;;) i've heard it's more efficient, but how does that work?
Link to comment
https://www.neowin.net/forum/topic/392657-loop-efficiency-comparison-whiletrue-vs-for/Share on other sites
3 answers to this question
Recommended Posts