引用请注明出处:http://blog.youkuaiyun.com/int64ago/article/details/7367270
今天面试被问到for(;;)和while(1)的效率问题,两个都是死循环,我开始主观的认为for(;;)里面有三次运算(或两次),效率当然是while(1)高,但是被告知错了,回来生成汇编看了下:
1、while(1)汇编
.file "test_effect.c"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
.L2:
jmp .L2
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 4.6.2 20111027 (Red Hat 4.6.2-1)"
.section .note.GNU-stack,"",@progbits
2、for(;;)
.file "test_effect.c"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
.L2:
jmp .L2
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 4.6.2 20111027 (Red Hat 4.6.2-1)"
.section .note.GNU-stack,"",@progbits
貌似没有任何区别,当然这是gcc汇编的,其它编译器就不知道了,至少对于gcc而言,两者没有区别!