你会用while(1)还是for(;;)写循环代码?

开发 后端
看代码看到for(;;),然后觉得为什么不写成while(1)呢,所以就做了下面的测试。

 看代码看到for(;;),然后觉得为什么不写成while(1)呢,所以就做了下面的测试。

网上有解释,因为while需要做一次判断,理论上执行会花费的时间更久,for(;;)只是执行了两次空语句,执行会更快

for.c 

  1. #include <stdio.h>  
  2. int main(){  
  3.    for(;;)  
  4.       printf("This is a loop\n");  
  5.    return 0;  

while.c 

  1. #include <stdio.h>  
  2. int main(){  
  3.    while(1)  
  4.       printf("This is a loop\n");  
  5.    return 0;  

goto.c 

  1. #include <stdio.h>  
  2. int main(){  
  3.    start:  
  4.       printf("This is a loop\n");  
  5.     goto start;  
  6.    return 0;  

用gcc -S xxx.c 执行后得到三个文件

for.s 

  1.  .file "for.c"  
  2.  .text  
  3.  .section .rodata  
  4. .LC0:  
  5.  .string "This is a loop"  
  6.  .text  
  7.  .globl main  
  8.  .type main, @function  
  9. main:  
  10. .LFB0:  
  11.  .cfi_startproc  
  12.  pushq %rbp  
  13.  .cfi_def_cfa_offset 16  
  14.  .cfi_offset 6, -16  
  15.  movq %rsp, %rbp  
  16.  .cfi_def_cfa_register 6  
  17. .L2:  
  18.  leaq .LC0(%rip), %rdi  
  19.  call puts@PLT  
  20.  jmp .L2  
  21.  .cfi_endproc  
  22. .LFE0:  
  23.  .size main, .-main  
  24.  .ident "GCC: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0"  
  25.  .section .note.GNU-stack,"",@progbits 

while.s 

  1.  .file "while.c"  
  2.  .text  
  3.  .section .rodata  
  4. .LC0:  
  5.  .string "This is a loop"  
  6.  .text  
  7.  .globl main  
  8.  .type main, @function  
  9. main:  
  10. .LFB0:  
  11.  .cfi_startproc  
  12.  pushq %rbp  
  13.  .cfi_def_cfa_offset 16  
  14.  .cfi_offset 6, -16  
  15.  movq %rsp, %rbp  
  16.  .cfi_def_cfa_register 6  
  17. .L2:  
  18.  leaq .LC0(%rip), %rdi  
  19.  call puts@PLT  
  20.  jmp .L2  
  21.  .cfi_endproc  
  22. .LFE0:  
  23.  .size main, .-main  
  24.  .ident "GCC: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0"  
  25.  .section .note.GNU-stack,"",@progbits 

goto.s 

  1.  .file "goto.c"  
  2.  .text  
  3.  .section .rodata  
  4. .LC0:  
  5.  .string "This is a loop"  
  6.  .text  
  7.  .globl main  
  8.  .type main, @function  
  9. main:  
  10. .LFB0:  
  11.  .cfi_startproc  
  12.  pushq %rbp  
  13.  .cfi_def_cfa_offset 16  
  14.  .cfi_offset 6, -16  
  15.  movq %rsp, %rbp  
  16.  .cfi_def_cfa_register 6  
  17. .L2:  
  18.  leaq .LC0(%rip), %rdi  
  19.  call puts@PLT  
  20.  jmp .L2  
  21.  .cfi_endproc  
  22. .LFE0:  
  23.  .size main, .-main  
  24.  .ident "GCC: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0"  
  25.  .section .note.GNU-stack,"",@progbits 

gcc 版本 

  1. gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0  
  2. Copyright (C) 2017 Free Software Foundation, Inc.  
  3. This is free software; see the source for copying conditions.  There is NO  
  4. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

在上面测试结束后,我还特意打开了我的keil软件,结果发现两个生成的机器码都是一样的。

所以说,如果在项目中遇到这样的写法,就不要再感觉奇怪了,他们都是没啥问题的。

只不过for(;;)看起来更优雅一些。

还有一种情况while(1)里面的1是一个常量,在一些编译器中,设置的检查规则比较高的话,会提示一个警告,for(;;)就不会存在这种问题,因为里面就没有变量,也没有常量。 

 

责任编辑:庞桂玉 来源: C语言与C++编程
相关推荐

2021-01-28 09:55:50

while(1)for(;;)Linux

2020-12-11 05:57:01

Python循环语句代码

2019-07-25 12:46:32

Java高并发编程语言

2021-03-04 21:57:12

Python编程语言计算

2021-03-24 13:17:41

编程循环语句Java

2010-09-08 17:00:22

SQLWHILE循环

2015-07-17 10:02:48

写代码

2018-04-17 11:47:06

if代码参数

2014-11-11 14:52:28

程序员工程师

2021-05-06 05:30:33

JSONstringify()parse()

2020-03-06 10:25:10

注解Java代码

2022-11-07 17:50:36

2010-01-07 15:42:57

VB.NET WhilEnd While循环

2019-11-26 09:45:27

软件设计设计模式

2021-12-09 23:20:31

Python循环语句

2020-02-20 10:45:57

代码JS开发

2022-09-30 07:32:48

循环while循环体

2010-09-09 16:34:19

SQL循环while

2016-05-04 10:36:42

iossdwebimage开发

2022-04-28 21:53:52

TypeScriptany类型
点赞
收藏

51CTO技术栈公众号