偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

你會(huì)用while(1)還是for(;;)寫循環(huán)代碼?

系統(tǒng) Linux
看代碼看到for(;;),然后覺得為什么不寫成while(1)呢,所以就做了下面的測試。一起來看看吧。

 看代碼看到for(;;),然后覺得為什么不寫成while(1)呢,所以就做了下面的測試。

網(wǎng)上有解釋,因?yàn)閣hile需要做一次判斷,理論上執(zhí)行會(huì)花費(fèi)的時(shí)間更久,for(;;)只是執(zhí)行了兩次空語句,執(zhí)行會(huì)更快

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 執(zhí)行后得到三個(gè)文件

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. 

在上面測試結(jié)束后,我還特意打開了我的keil軟件,結(jié)果發(fā)現(xiàn)兩個(gè)生成的機(jī)器碼都是一樣的。

所以說,如果在項(xiàng)目中遇到這樣的寫法,就不要再感覺奇怪了,他們都是沒啥問題的。

只不過for(;;)看起來更優(yōu)雅一些。

還有一種情況while(1)里面的1是一個(gè)常量,在一些編譯器中,設(shè)置的檢查規(guī)則比較高的話,會(huì)提示一個(gè)警告,for(;;)就不會(huì)存在這種問題,因?yàn)槔锩婢蜎]有變量,也沒有常量。 

 

責(zé)任編輯:龐桂玉 來源: 良許Linux
相關(guān)推薦

2021-03-17 11:16:58

while(1)for(;;)語言

2020-12-11 05:57:01

Python循環(huán)語句代碼

2019-07-25 12:46:32

Java高并發(fā)編程語言

2021-03-04 21:57:12

Python編程語言計(jì)算

2024-08-22 08:17:55

C#工具循環(huán)

2021-03-24 13:17:41

編程循環(huán)語句Java

2021-05-06 05:30:33

JSONstringify()parse()

2015-07-17 10:02:48

寫代碼

2018-04-17 11:47:06

if代碼參數(shù)

2014-11-11 14:52:28

程序員工程師

2010-09-08 17:00:22

SQLWHILE循環(huán)

2022-11-07 17:50:36

2020-03-06 10:25:10

注解Java代碼

2016-05-04 10:36:42

iossdwebimage開發(fā)

2022-04-28 21:53:52

TypeScriptany類型

2024-07-05 15:26:59

代碼Merge分支

2019-11-26 09:45:27

軟件設(shè)計(jì)設(shè)計(jì)模式

2020-02-20 10:45:57

代碼JS開發(fā)

2024-05-16 12:23:45

C語言指針代碼

2010-01-07 15:42:57

VB.NET WhilEnd While循環(huán)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)