最好的无限循环 [英] Best infinite loop
问题描述
可能重复:结果
而(1)比。为(;;)是否有转速差?
这是更好,更快,更优化,能够实现无限循环的方式 - 为(;;)或在(1)?为什么?
Which is better,faster and more optimized way to implement infinite loop - for(;;) or while(1)? and why?
推荐答案
在任何正常的编译器,应该是绝对没有区别。例如,这里就是 LLVM-铛
生成(使用 -O3
标记)的而(1){}
:
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
请注意在 JMP .LBB0_1
的一部分,这是实际的无限循环。对于为(;;)
样,它生成的绝对是一样的code 的
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,我不得不尝试用 GCC
以及
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
这篇关于最好的无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!