- 浏览: 668398 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (297)
- J2SE (78)
- swt/飞信 (20)
- mysql/mssql (17)
- 设计模式 (5)
- windows (18)
- 闲言碎语 (19)
- struts 1.x (6)
- JVM (6)
- tomcat/jetty (8)
- jquery/javascript (15)
- web前端 (6)
- J2EE (0)
- PHP (6)
- 算法设计 (17)
- 数据结构 (3)
- C/C++ (6)
- linux (19)
- 程序打包 (8)
- eclipse/myeclipse (10)
- 其他杂项 (13)
- 应聘 (9)
- spring/spring mvc (4)
- Maven/Ant (2)
- ERROR (1)
- nosql/hbase (1)
- hibernate (3)
- Solr/Lucene (1)
最新评论
-
乔木1937:
太感谢了,看到你的文章终于解决这个问题了!
[转载]通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connection refused: connect。 -
xianweisi:
竟然还有马
精简JRE - 实例Swing计算器 with 精简JRE(续) -
Javkburd:
我刚也遇到这个问题,然后也把默认端口改成了1433,只差最后没 ...
[转载]通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connection refused: connect。 -
yeshaoting:
kingbinchow 写道 最近的爪哇岛 没有什么货进项呀 ...
jQuery方法区别(四)click() bind() live() delegate()区别 -
kingbinchow:
最近的爪哇岛 没有什么货进项呀!
jQuery方法区别(四)click() bind() live() delegate()区别
【C语言】while与for执行效率对比
C语言编译环境: Microsoft Visual C++ 6.0(SP6)
测试程序
#include "stdio.h" void forTest() { int num = 1234; // 迭代次数 long sum = 0; // 保存加法结果 for(int i=0;i<num;i++) // 传统for循环写法 { sum = sum + i; // 计算结果 } printf("forTest:%d\n",sum); } void whileTest() { int num = 1234; // 迭代次数 long sum = 0; // 保存加法结果 while((num--)>0) { sum = sum + num; // 计算结果 } printf("whileTest:%d\n",sum); } void main() { forTest(); whileTest(); }
汇编片段
whileTest()函数汇编后的指令:
--- D:\VC\ForWhile\ForWhile.cpp ------------------------------------------------------------------------------------------
17: void whileTest() 18: { 0040D760 push ebp 0040D761 mov ebp,esp 0040D763 sub esp,48h 0040D766 push ebx 0040D767 push esi 0040D768 push edi 0040D769 lea edi,[ebp-48h] 0040D76C mov ecx,12h 0040D771 mov eax,0CCCCCCCCh 0040D776 rep stos dword ptr [edi] 19: int num = 1234; 0040D778 mov dword ptr [ebp-4],4D2h 20: long sum = 0; 0040D77F mov dword ptr [ebp-8],0 21: 22: while((num--)>0) 0040D786 mov eax,dword ptr [ebp-4] 0040D789 mov ecx,dword ptr [ebp-4] 0040D78C sub ecx,1 0040D78F mov dword ptr [ebp-4],ecx 0040D792 test eax,eax 0040D794 jle whileTest+41h (0040d7a1) 23: { 24: sum = sum + num; 0040D796 mov edx,dword ptr [ebp-8] 0040D799 add edx,dword ptr [ebp-4] 0040D79C mov dword ptr [ebp-8],edx 25: } 0040D79F jmp whileTest+26h (0040d786) 26: 27: printf("whileTest:%d\n",sum); 0040D7A1 mov eax,dword ptr [ebp-8] 0040D7A4 push eax 0040D7A5 push offset string "whileTest:%d\n" (00422fac) 0040D7AA call printf (0040d6e0) 0040D7AF add esp,8 28: } 0040D7B2 pop edi 0040D7B3 pop esi 0040D7B4 pop ebx 0040D7B5 add esp,48h 0040D7B8 cmp ebp,esp 0040D7BA call __chkesp (0040d6a0) 0040D7BF mov esp,ebp 0040D7C1 pop ebp 0040D7C2 ret
--- No source file -------------------------------------------------------------------------------------------------------
分析:
0040D760~0040D776: 保存栈现场 总共10条指令
0040D778: 迭代次数 总共1条指令
0040D77F: 保存加法结果 总共1条指令
0040D786~0040D79F: while循环 总共10条指令
0040D7A1~0040D7AF: 打印结果 总共5条指令
0040D7B2~0040D7C2: 恢复栈现场 总共9条指令
合计: 36条指令
forTest()函数汇编后的指令:
--- D:\VC\ForWhile\ForWhile.cpp ------------------------------------------------------------------------------------------
4: void forTest() 5: { 0040D3F0 push ebp 0040D3F1 mov ebp,esp 0040D3F3 sub esp,4Ch 0040D3F6 push ebx 0040D3F7 push esi 0040D3F8 push edi 0040D3F9 lea edi,[ebp-4Ch] 0040D3FC mov ecx,13h 0040D401 mov eax,0CCCCCCCCh 0040D406 rep stos dword ptr [edi] 6: int num = 1234; 0040D408 mov dword ptr [ebp-4],4D2h 7: long sum = 0; 0040D40F mov dword ptr [ebp-8],0 8: 9: for(int i=0;i<num;i++) 0040D416 mov dword ptr [ebp-0Ch],0 0040D41D jmp forTest+38h (0040d428) 0040D41F mov eax,dword ptr [ebp-0Ch] 0040D422 add eax,1 0040D425 mov dword ptr [ebp-0Ch],eax 0040D428 mov ecx,dword ptr [ebp-0Ch] 0040D42B cmp ecx,dword ptr [ebp-4] 0040D42E jge forTest+4Bh (0040d43b) 10: { 11: sum = sum + i; 0040D430 mov edx,dword ptr [ebp-8] 0040D433 add edx,dword ptr [ebp-0Ch] 0040D436 mov dword ptr [ebp-8],edx 12: } 0040D439 jmp forTest+2Fh (0040d41f) 13: 14: printf("forTest:%d\n",sum); 0040D43B mov eax,dword ptr [ebp-8] 0040D43E push eax 0040D43F push offset string "forTest:%l\n" (00422e80) 0040D444 call printf (0040d6e0) 0040D449 add esp,8 15: } 0040D44C pop edi 0040D44D pop esi 0040D44E pop ebx 0040D44F add esp,4Ch 0040D452 cmp ebp,esp 0040D454 call __chkesp (0040d6a0) 0040D459 mov esp,ebp 0040D45B pop ebp 0040D45C ret
--- No source file -------------------------------------------------------------------------------------------------------
分析:
0040D3F0~0040D406: 保存栈现场 总共10条指令
0040D408: 迭代次数 总共1条指令
0040D40F: 保存加法结果 总共1条指令
0040D416~0040D439: for循环 总共12条指令
0040D43B~0040D449: 打印结果 总共5条指令
0040D44C~0040D45C: 恢复栈现场 总共9条指令
合计: 38条指令
程序中二个方法语句区别在于一个是for循环,一个是while循环.
对应于,查看到上述二段汇编指令段while循环比for循环少了二条指令.
程序中for循环用的是传统写法,做下更改将for(int i=0;i<num;i++)改为for(;(num--)>0;),其汇编指令为:
--- D:\VC\ForWhile\ForWhile.cpp ------------------------------------------------------------------------------------------
4: void forTest() 5: { 0040D3F0 push ebp 0040D3F1 mov ebp,esp 0040D3F3 sub esp,48h 0040D3F6 push ebx 0040D3F7 push esi 0040D3F8 push edi 0040D3F9 lea edi,[ebp-48h] 0040D3FC mov ecx,12h 0040D401 mov eax,0CCCCCCCCh 0040D406 rep stos dword ptr [edi] 6: int num = 1234; 0040D408 mov dword ptr [ebp-4],4D2h 7: long sum = 0; 0040D40F mov dword ptr [ebp-8],0 8: 9: for(;(num--)>0;) 0040D416 mov eax,dword ptr [ebp-4] 0040D419 mov ecx,dword ptr [ebp-4] 0040D41C sub ecx,1 0040D41F mov dword ptr [ebp-4],ecx 0040D422 test eax,eax 0040D424 jle forTest+41h (0040d431) 10: { 11: sum = sum + num; 0040D426 mov edx,dword ptr [ebp-8] 0040D429 add edx,dword ptr [ebp-4] 0040D42C mov dword ptr [ebp-8],edx 12: } 0040D42F jmp forTest+26h (0040d416) 13: 14: printf("forTest:%d\n",sum); 0040D431 mov eax,dword ptr [ebp-8] 0040D434 push eax 0040D435 push offset string "forTest:%l\n" (00422e80) 0040D43A call printf (0040d6e0) 0040D43F add esp,8 15: } 0040D442 pop edi 0040D443 pop esi 0040D444 pop ebx 0040D445 add esp,48h 0040D448 cmp ebp,esp 0040D44A call __chkesp (0040d6a0) 0040D44F mov esp,ebp 0040D451 pop ebp 0040D452 ret
--- No source file -------------------------------------------------------------------------------------------------------
0040D3F0~0040D406: 保存栈现场 总共10条指令
0040D408: 迭代次数 总共1条指令
0040D40F: 保存加法结果 总共1条指令
0040D416~0040D42F: for循环 总共10条指令
0040D431~0040D43F: 打印结果 总共5条指令
0040D442~0040D452: 恢复栈现场 总共9条指令
合计: 36条指令
由此可见,for循环习惯写法for(int i=0;i<num;i++)执行效率低于for(;(num--)>0;)写法,而for(;(num--)>0;)写法执行效率与while((num--)>0)相同.
因此,一棒子打死说for循环执行效率比while循环慢是不对的.
评论
围观?
都老贴了
这个没有研究,有空看看 .
VC6实在是太旧了.
好吧...
对编译优化不太了解,了解后再进行对比吧.
VC6实在是太旧了.
这么高深的东西还不会。
还有待进一步提高~---
<div class="quote_div">
<p>非常不错.学习啦..</p>
</div>
<p>哈哈...</p>
对这些法则了解不深...
学设计模式的时候也就学了点皮毛.
版块也没必要区分太细.
因为之前写了一篇【Java语言】while与for执行效率对比,然后想看看C里面的情况,就有了这篇.
后面的只是附加之笔.
见谅,我们可以继续这个话题.
不知道分析的是否全面....
<div class="quote_div">
<p> </p>
<div class="quote_title"> 写道</div>
<div class="quote_div">public static int testFor(){<br> int i;<br> int sum = 0;<br> for (i = 0;i < 100;i++){<br> sum = sum + i;<br> }<br> return sum;<br> }<br> public static int testWhile(){<br> int i;<br> int sum = 0;<br> i = 0;<br> while ( i < 100){<br> sum = sum + i;<br> i ++;<br> }<br> return sum;<br> }</div>
<p>字节码指令为:</p>
<p> </p>
<pre name="code" class="java">public static int testFor();
Code:
0: iconst_0
1: istore_1
2: iconst_0
3: istore_0
4: goto 14
7: iload_1
8: iload_0
9: iadd
10: istore_1
11: iinc 0, 1
14: iload_0
15: bipush 100
17: if_icmplt 7
20: iload_1
21: ireturn
public static int testWhile();
Code:
0: iconst_0
1: istore_1
2: iconst_0
3: istore_0
4: goto 14
7: iload_1
8: iload_0
9: iadd
10: istore_1
11: iinc 0, 1
14: iload_0
15: bipush 100
17: if_icmplt 7
20: iload_1
21: ireturn
</pre>
<p>
完全一样</p>
</div>
<p>也是通过字节码得出来的结果呀.</p>
<p>我还以为有什么其他的理论呢.</p>
<p>不错~~</p>
<div class="quote_title"> 写道</div>
<div class="quote_div">public static int testFor(){<br> int i;<br> int sum = 0;<br> for (i = 0;i < 100;i++){<br> sum = sum + i;<br> }<br> return sum;<br> }<br> public static int testWhile(){<br> int i;<br> int sum = 0;<br> i = 0;<br> while ( i < 100){<br> sum = sum + i;<br> i ++;<br> }<br> return sum;<br> }</div>
<p>字节码指令为:</p>
<p> </p>
<pre name="code" class="java">public static int testFor();
Code:
0: iconst_0
1: istore_1
2: iconst_0
3: istore_0
4: goto 14
7: iload_1
8: iload_0
9: iadd
10: istore_1
11: iinc 0, 1
14: iload_0
15: bipush 100
17: if_icmplt 7
20: iload_1
21: ireturn
public static int testWhile();
Code:
0: iconst_0
1: istore_1
2: iconst_0
3: istore_0
4: goto 14
7: iload_1
8: iload_0
9: iadd
10: istore_1
11: iinc 0, 1
14: iload_0
15: bipush 100
17: if_icmplt 7
20: iload_1
21: ireturn
</pre>
<p>
完全一样</p>
兄台为何如此肯定.
给讲讲呗.
发表评论
-
sizeof函数与表达式操作无关
2012-03-21 16:12 1642引子: 在3 ... -
C++函数的调用与参数传递
2011-11-01 10:36 2008C++函数的调用与参数传递 根据函数参数传递的 ... -
百度一面算法题(字符串拷贝)
2011-10-24 12:32 1216完成下述字符串拷贝函数,要求: 1. 将s ... -
百度一面算法题(字符串拷贝)
2011-10-24 12:31 0完成下述字符串拷贝函数,要求: 1. 将src ... -
百度一面算法题(字符串拷贝)
2011-10-10 13:09 15完成下述字符串拷贝函数,要求: 1. 将src_str ... -
[C/C++]华为机试题 2011-9-17
2011-09-18 09:24 1518问题描述: 过滤输入字符串中的重复字符. ... -
[不同编译器]函数参数求值顺序分析(附前置与后置++执行效率)
2011-03-04 11:52 1639[不同编译器]函数参数求值顺序分析(附前置与后置++执行效率) ...
相关推荐
浅论C语言在提高程序执行效率上的编程技巧.pdf
While循环和for循环在C语言里面经常使用,需要讲的地方不多,可以一句话带过。加上while循环和for循环是可以相互完全替代的,所以这里主要讲一下while循环()括号里面可能是何种实现方法,从API的角度来看待while...
C语言while、do-while、for循环 课程5
c语言之while循环PPT课件.pptx
c语言之while循环学习教案.pptx
1.学会使用while、do-while、for语句2.熟练掌握循环结构程序设计方法3.掌握选择结构与循环结构的嵌套
C语言实现求1+2+3+……+100的和,要求分别用while、do while、for循环实现。.txt
这是一个99乘法表,希望初学c语言的童鞋都能学有所成!
本文主要介绍了关于c语言中的for循环程序流程图。
C语言嵌套for循环详细讲解
C语言While语句[借鉴].pdf
c语言-while循环
C语言实现 各种排序包括: 直接插入排序,折半插入排序,2—路插入排序和表插入排序;希尔排序和链式基数排序;起泡排序,快速排序,归并排序;简单选择排序,树形选择排序和堆排序。 通过输入不同的数据量和数据...
C语言while循环教(学)案.pdf
本文主要介绍C语言while 语句,这里对while 循环做详细讲解,并提供示例代码,希望能帮助学习C语言的同学
C语言while循环教(学)案.doc
C语言版文件相似度对比,文本对比 数据结构C语言大作业 采用递归矩阵对比,用了结构体
C语言中的for语句,对于某个特定任务我们可以采用多种方法来编写程序。下面这段代码也可以实现前面的温度转换程序的功能
51单片机-C语言for语句do...while语句if...else...语句产生随机数rand()常用基本语句例程源码 do...while语句 for语句 if...else...语句 switch-case-break语句 while与do...while区别 while语句 不同类型数据的差异...
主要是介绍汇编与c语言的对比使用情况,让读者能清晰的比较两者的关系。