静态网页设计网站制作,设计类比赛网站,天元建设集团坑人,如何网站后台清理缓存看代码看到for(;;)#xff0c;然后觉得为什么不写成while(1)呢#xff0c;所以就做了下面的测试。网上有解释#xff0c;因为while需要做一次判断#xff0c;理论上执行会花费的时间更久#xff0c;for(;;)只是执行了两次空语句#xff0c;执行会更快for.c#include s… 看代码看到for(;;)然后觉得为什么不写成while(1)呢所以就做了下面的测试。网上有解释因为while需要做一次判断理论上执行会花费的时间更久for(;;)只是执行了两次空语句执行会更快for.c#include stdio.h
int main(){for(;;)printf(This is a loop\n);return 0;
}
while.c#include stdio.h
int main(){while(1)printf(This is a loop\n);return 0;
}
goto.c#include stdio.h
int main(){start:printf(This is a loop\n);goto start;return 0;
}
用gcc -S xxx.c 执行后得到三个文件for.s .file for.c.text.p .rodata
.LC0:.string This is a loop.text.globl main.type main, function
main:
.LFB0:.cfi_startprocpushq %rbp.cfi_def_cfa_offset 16.cfi_offset 6, -16movq %rsp, %rbp.cfi_def_cfa_register 6
.L2:leaq .LC0(%rip), %rdicall putsPLTjmp .L2.cfi_endproc
.LFE0:.size main, .-main.ident GCC: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0.p .note.GNU-stack,,progbitswhile.s .file while.c.text.p .rodata
.LC0:.string This is a loop.text.globl main.type main, function
main:
.LFB0:.cfi_startprocpushq %rbp.cfi_def_cfa_offset 16.cfi_offset 6, -16movq %rsp, %rbp.cfi_def_cfa_register 6
.L2:leaq .LC0(%rip), %rdicall putsPLTjmp .L2.cfi_endproc
.LFE0:.size main, .-main.ident GCC: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0.p .note.GNU-stack,,progbitsgoto.s .file goto.c.text.p .rodata
.LC0:.string This is a loop.text.globl main.type main, function
main:
.LFB0:.cfi_startprocpushq %rbp.cfi_def_cfa_offset 16.cfi_offset 6, -16movq %rsp, %rbp.cfi_def_cfa_register 6
.L2:leaq .LC0(%rip), %rdicall putsPLTjmp .L2.cfi_endproc
.LFE0:.size main, .-main.ident GCC: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0.p .note.GNU-stack,,progbitsgcc 版本gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
在上面测试结束后我还特意打开了我的keil软件结果发现两个生成的机器码都是一样的。所以说如果在项目中遇到这样的写法就不要再感觉奇怪了他们都是没啥问题的。只不过for(;;)看起来更优雅一些。还有一种情况while(1)里面的1是一个常量在一些编译器中设置的检查规则比较高的话会提示一个警告for(;;)就不会存在这种问题因为里面就没有变量也没有常量。推荐阅读专辑|Linux文章汇总专辑|程序人生专辑|C语言我的知识小密圈