【C开发】无限循环 while(1) 和 for(; ;)

来源:互联网 发布:全日制本科助学班 知乎 编辑:程序博客网 时间:2024/05/16 01:57

无限循环有两种常用的方法:

while(1) 和 for(; ; ) 。

两种方法的效果一样,相比之下,哪种更好些?

编译后代码对比:

1、while( 1 );

00401028 mov eax,10040102D test eax,eax0040102F je main+23h (00401033)00401031 jmp main+18h (00401028)

 2、for( ; ; );

00401033 jmp main+23h (00401033)

对比发现,for(; ;)指令少,不占用寄存器,而且没有判断、跳转,比while( 1 )要好一些。

 

PS:在VC6.0中,设断点调试,菜单View -> Debug Windows ->  Disassembly即可查看编译后代码。

 

 

 


<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>