|
看代码看到for(;;),然后觉得为什么不写成while(1)呢,所以就做了下面的测试。 网上有解释,因为while需要做一次判断,理论上执行会花费的时间更久,for(;;)只是执行了两次空语句,执行会更快 for.c #include <stdio.h>int main(){ for(;;) printf("This is a loop\n"); return 0;( S5 n4 T4 V$ I/ U% ~6 d7 y$ H } while.c #include <stdio.h>int main(){ while(1) printf("This is a loop\n");# r) k1 f+ p; H1 q8 \ return 0;. p7 Q: c$ s2 B3 } } goto.c #include <stdio.h>int main(){3 O7 F# ~, J+ j- | start:1 v. ?0 I; u+ m, F9 i J printf("This is a loop\n");3 Z( A' e- e1 v% y/ ~& g/ T goto start; return 0;7 _2 @. b0 `# I$ d ]# W+ F } H3 f; B* }. W0 y" |; z 用gcc -S xxx.c 执行后得到三个文件 for.s .file "for.c".text .section .rodata .LC0:+ B! \$ ^% M! a0 d/ M .string "This is a loop" .text0 [2 @& {4 c/ o, v. E .globl main .type main, @function main: .LFB0:% U$ H) T* q* v1 N7 Z .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp$ N& n W. ]# g& `9 r7 y .cfi_def_cfa_register 62 b* O* C0 `6 I8 I/ E8 W" a h, o .L2:5 K' _; \9 [" i- A! u, U leaq .LC0(%rip), %rdi! A6 Q& i2 h1 l5 z. M# z" j7 x call puts@PLT+ H) n3 j" w" y7 P3 X0 C @ jmp .L2 .cfi_endproc8 I/ @5 K" U; v3 ~+ m .LFE0:+ A" E3 R* o o/ b) [( ^ .size main, .-main b# n' k- N" I2 V4 W! [ .ident "GCC: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0"+ O+ K3 l4 V/ n; N/ d/ L" i .section .note.GNU-stack,"",@progbits% { q! b% ~9 I while.s .file "while.c"# b9 O" P- F- ~* k' ^5 R.text .section .rodata .LC0:0 N. Z* f$ v) j2 ]" H( t& s( K5 q .string "This is a loop"7 ? s6 b7 N, y# W9 N5 ~! G0 [! x8 f .text ` g8 g9 Z- Y8 w6 D .globl main .type main, @function main: .LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16) W: W6 K7 ?9 ^ .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 .L2: leaq .LC0(%rip), %rdi8 E) Q) O: e* @( s* b call puts@PLT4 J. ]$ D- T9 ^; g% g) N7 O% L+ r jmp .L2 u# e4 c4 @- X6 n, J6 }$ L .cfi_endproc .LFE0: .size main, .-main P3 \1 O/ i5 w0 T: V9 V: w1 u .ident "GCC: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0"+ \7 Q' C1 p# Y7 K1 x/ s .section .note.GNU-stack,"",@progbits& P! k5 h3 N6 B% s0 ] goto.s .file "goto.c".text .section .rodata .LC0: .string "This is a loop" .text# I" U# n/ T, `- q3 P% S .globl main .type main, @function) i' R+ ]; j: ` _( b main:* c5 @, K: |) f6 z: @4 ] .LFB0:" G, m/ R' [6 B; S/ k7 K+ k .cfi_startproc( |5 B% M9 J2 p$ {. V pushq %rbp* r0 {& l3 ^8 W: `7 C0 Q: M .cfi_def_cfa_offset 168 f8 B4 n, Y7 t$ `& _ .cfi_offset 6, -16 movq %rsp, %rbp. D9 W! Y6 P- d. C M/ R- y; t d( O .cfi_def_cfa_register 6 .L2:! z8 G$ K2 X% d2 Y( Q leaq .LC0(%rip), %rdi" l7 |. H" j; y( @! E. L call puts@PLT jmp .L2 .cfi_endproc$ \3 v% Y( h2 v# X4 X3 ^ .LFE0: .size main, .-main .ident "GCC: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0" .section .note.GNU-stack,"",@progbits * N6 z- T5 o# b- t; E gcc 版本 gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0Copyright (C) 2017 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO& x& q* Z6 B( V warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 在上面测试结束后,我还特意打开了我的keil软件,结果发现两个生成的机器码都是一样的。 所以说,如果在项目中遇到这样的写法,就不要再感觉奇怪了,他们都是没啥问题的。 只不过for(;;)看起来更优雅一些。
还有一种情况while(1)里面的1是一个常量,在一些编译器中,设置的检查规则比较高的话,会提示一个警告,for(;;)就不会存在这种问题,因为里面就没有变量,也没有常量。 " e B* \1 h W |
微信公众号
手机版