您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > 【C语言】while与for执行效率的比较
【C语言】while与for执行效率对比QQ1724920991C语言编译环境:MicrosoftVisualC++6.0(SP6)测试程序C代码1.#includestdio.h2.3.voidforTest()4.{5.intnum=1234;//迭代次数6.longsum=0;//保存加法结果7.8.for(inti=0;inum;i++)//传统for循环写法9.{10.sum=sum+i;//计算结果11.}12.13.printf(forTest:%d\n,sum);14.}15.16.voidwhileTest()17.{18.intnum=1234;//迭代次数19.longsum=0;//保存加法结果20.21.while((num--)0)22.{23.sum=sum+num;//计算结果24.}25.26.printf(whileTest:%d\n,sum);27.}28.29.30.voidmain()31.{32.forTest();33.whileTest();34.}汇编片段whileTest()函数汇编后的指令:---D:\VC\ForWhile\ForWhile.cpp------------------------------------------------------------------------------------------汇编指令代码1.17:voidwhileTest()2.18:{3.0040D760pushebp4.0040D761movebp,esp5.0040D763subesp,48h6.0040D766pushebx7.0040D767pushesi8.0040D768pushedi9.0040D769leaedi,[ebp-48h]10.0040D76Cmovecx,12h11.0040D771moveax,0CCCCCCCCh12.0040D776repstosdwordptr[edi]13.19:intnum=1234;14.0040D778movdwordptr[ebp-4],4D2h15.20:longsum=0;16.0040D77Fmovdwordptr[ebp-8],017.21:18.22:while((num--)0)19.0040D786moveax,dwordptr[ebp-4]20.0040D789movecx,dwordptr[ebp-4]21.0040D78Csubecx,122.0040D78Fmovdwordptr[ebp-4],ecx23.0040D792testeax,eax24.0040D794jlewhileTest+41h(0040d7a1)25.23:{26.24:sum=sum+num;27.0040D796movedx,dwordptr[ebp-8]28.0040D799addedx,dwordptr[ebp-4]29.0040D79Cmovdwordptr[ebp-8],edx30.25:}31.0040D79FjmpwhileTest+26h(0040d786)32.26:33.27:printf(whileTest:%d\n,sum);34.0040D7A1moveax,dwordptr[ebp-8]35.0040D7A4pusheax36.0040D7A5pushoffsetstringwhileTest:%d\n(00422fac)37.0040D7AAcallprintf(0040d6e0)38.0040D7AFaddesp,839.28:}40.0040D7B2popedi41.0040D7B3popesi42.0040D7B4popebx43.0040D7B5addesp,48h44.0040D7B8cmpebp,esp45.0040D7BAcall__chkesp(0040d6a0)46.0040D7BFmovesp,ebp47.0040D7C1popebp48.0040D7C2ret---Nosourcefile-------------------------------------------------------------------------------------------------------分析:0040D760~0040D776:保存栈现场总共10条指令0040D778:迭代次数总共1条指令0040D77F:保存加法结果总共1条指令0040D786~0040D79F:while循环总共10条指令0040D7A1~0040D7AF:打印结果总共5条指令0040D7B2~0040D7C2:恢复栈现场总共9条指令合计:36条指令forTest()函数汇编后的指令:---D:\VC\ForWhile\ForWhile.cpp------------------------------------------------------------------------------------------汇编指令代码1.4:voidforTest()2.5:{3.0040D3F0pushebp4.0040D3F1movebp,esp5.0040D3F3subesp,4Ch6.0040D3F6pushebx7.0040D3F7pushesi8.0040D3F8pushedi9.0040D3F9leaedi,[ebp-4Ch]10.0040D3FCmovecx,13h11.0040D401moveax,0CCCCCCCCh12.0040D406repstosdwordptr[edi]13.6:intnum=1234;14.0040D408movdwordptr[ebp-4],4D2h15.7:longsum=0;16.0040D40Fmovdwordptr[ebp-8],017.8:18.9:for(inti=0;inum;i++)19.0040D416movdwordptr[ebp-0Ch],020.0040D41DjmpforTest+38h(0040d428)21.0040D41Fmoveax,dwordptr[ebp-0Ch]22.0040D422addeax,123.0040D425movdwordptr[ebp-0Ch],eax24.0040D428movecx,dwordptr[ebp-0Ch]25.0040D42Bcmpecx,dwordptr[ebp-4]26.0040D42EjgeforTest+4Bh(0040d43b)27.10:{28.11:sum=sum+i;29.0040D430movedx,dwordptr[ebp-8]30.0040D433addedx,dwordptr[ebp-0Ch]31.0040D436movdwordptr[ebp-8],edx32.12:}33.0040D439jmpforTest+2Fh(0040d41f)34.13:35.14:printf(forTest:%d\n,sum);36.0040D43Bmoveax,dwordptr[ebp-8]37.0040D43Epusheax38.0040D43FpushoffsetstringforTest:%l\n(00422e80)39.0040D444callprintf(0040d6e0)40.0040D449addesp,841.15:}42.0040D44Cpopedi43.0040D44Dpopesi44.0040D44Epopebx45.0040D44Faddesp,4Ch46.0040D452cmpebp,esp47.0040D454call__chkesp(0040d6a0)48.0040D459movesp,ebp49.0040D45Bpopebp50.0040D45Cret---Nosourcefile-------------------------------------------------------------------------------------------------------分析:0040D3F0~0040D406:保存栈现场总共10条指令0040D408:迭代次数总共1条指令0040D40F:保存加法结果总共1条指令0040D416~0040D439:for循环总共12条指令0040D43B~0040D449:打印结果总共5条指令0040D44C~0040D45C:恢复栈现场总共9条指令合计:38条指令程序中二个方法语句区别在于一个是for循环,一个是while循环.对应于,查看到上述二段汇编指令段while循环比for循环少了二条指令.程序中for循环用的是传统写法,做下更改将for(inti=0;inum;i++)改为for(;(num--)0;),其汇编指令为:---D:\VC\ForWhile\ForWhile.cpp------------------------------------------------------------------------------------------汇编指令代码1.4:voidforTest()2.5:{3.0040D3F0pushebp4.0040D3F1movebp,esp5.0040D3F3subesp,48h6.0040D3F6pushebx7.0040D3F7pushesi8.0040D3F8pushedi9.0040D3F9leaedi,[ebp-48h]10.0040D3FCmovecx,12h11.0040D401moveax,0CCCCCCCCh12.0040D406repstosdwordptr[edi]13.6:intnum=1234;14.0040D408movdwordptr[ebp-4],4D2h15.7:longsum=0;16.0040D40Fmovdwordptr[ebp-8],017.8:18.9:for(;(num--)0;)19.0040D416moveax,dwordptr[ebp-4]20.0040D419movecx,dwordptr[ebp-4]21.0040D41Csubecx,122.0040D41Fmovdwordptr[ebp-4],ecx23.0040D422testeax,eax24.0040D424jleforTest+41h(0040d431)25.10:{26.11:sum=sum+num;27.0040D426movedx,dwordptr[ebp-8]28.0040D429addedx,dwordptr[ebp-4]29.0040D42Cmovdwordptr[ebp-8],edx30.12:}31.0040D42FjmpforTest+26h(0040d416)32.13:33.14:printf(forTest:%d\n,sum);34.0040D431moveax,dwordptr[ebp-8]35.0040D434pusheax36.0040D435pushoffsetstringforTest:%l\n(00422e80)37.0040D43Acallprintf(0040d6e0)38.0040D43Faddesp,839.15:}40.0040D442popedi41.0040D443popesi42.0040D444popebx43.0040D445addesp,48h44.0040D448cmpebp,esp45.0040D44Acall__chkesp(0040d6a0)46.0040D44Fmovesp,ebp47.0040D451popebp48.0040D452ret---Nosourcefile---------------------------------------------------------------------------
本文标题:【C语言】while与for执行效率的比较
链接地址:https://www.777doc.com/doc-2821279 .html