Programming 板


LINE

我用 VS2005 试了一下, 程式码分别用: void test() { for(i = 100; i >= 0; i--) s = s + i; } void test2() { for(i = 100; --i; ) s += i; } // 这样出来会是 4950, 不过意思到了就好 未开最佳化: test: for(i = 100; i >= 0; i--) 004130BE mov dword ptr [_i (417184h)],64h 004130C8 jmp test+37h (4130D7h) 004130CA mov eax,dword ptr [_i (417184h)] 004130CF sub eax,1 004130D2 mov dword ptr [_i (417184h)],eax 004130D7 cmp dword ptr [_i (417184h)],0 004130DE jl test+52h (4130F2h) s = s + i; 004130E0 mov eax,dword ptr [_s (417188h)] 004130E5 add eax,dword ptr [_i (417184h)] 004130EB mov dword ptr [_s (417188h)],eax 004130F0 jmp test+2Ah (4130CAh) test2: for(i = 100; --i; ) 0041340E mov dword ptr [_i (417184h)],64h 00413418 mov eax,dword ptr [_i (417184h)] 0041341D sub eax,1 00413420 mov dword ptr [_i (417184h)],eax 00413425 je test2+49h (413439h) s += i; 00413427 mov eax,dword ptr [_s (417188h)] 0041342C add eax,dword ptr [_i (417184h)] 00413432 mov dword ptr [_s (417188h)],eax 00413437 jmp test2+28h (413418h) 这边因为 for 少一个式子, 所以 test() 会简短一些 i-- 和 --i, 我记得在 i 是基本型别的时候没太大差别, 在 C++ 是物件的时候比较有差 开最佳化 (/O2): test: for(i = 100; i >= 0; i--) 00401020 mov ecx,dword ptr [s (403374h)] 00401026 mov eax,64h 0040102B jmp test+10h (401030h) 0040102D lea ecx,[ecx] s = s + i; 00401030 add ecx,eax 00401032 sub eax,1 00401035 jns test+10h (401030h) 00401037 mov dword ptr [i (403370h)],eax 0040103C mov dword ptr [s (403374h)],ecx test2: for(i = 100; --i; ) 00401050 mov ecx,dword ptr [s (403374h)] 00401056 mov eax,63h 0040105B jmp test2+10h (401060h) 0040105D lea ecx,[ecx] s += i; 00401060 add ecx,eax 00401062 sub eax,1 00401065 jne test2+10h (401060h) 00401067 mov dword ptr [i (403370h)],eax 0040106C mov dword ptr [s (403374h)],ecx 时间一样, 所以对於简单内建型别的东西, 编译器就可以处理大部份的最佳化 当然用一些更激进的写法会更快, 不过可能就会牺牲易读性 工作时间应该用来改进一些 compiler 没办法做的地方比较适合~ 新增 gcc 的部分, 第一次反组译 gcc 的执行档, 应该有混一些其他的东西进去 (gcc -O2 直接把函式 inline 了) 有开最佳化 (-O2): test: :00401050 55 push ebp :00401051 8B0D10304000 mov ecx, dword[00403010] :00401057 BA64000000 mov edx, 00000064 :0040105C 891514304000 mov dword[00403014], edx :00401062 89E5 mov ebp, esp :00401064 B864000000 mov eax, 00000064 :00401069 8DB42600000000 lea esi, dword[esi+00000000] --------- :00401070 8D1401 lea edx, dword[ecx+eax] :00401073 48 dec eax :00401074 89D1 mov ecx, edx :00401076 79F8 jns 00401070 :00401078 5D pop ebp :00401079 B8FFFFFFFF mov eax, FFFFFFFF :0040107E A314304000 mov dword[00403014], eax :00401083 891510304000 mov dword[00403010], edx :00401089 C3 ret test2: :00401050 55 push ebp :00401051 8B0D10304000 mov ecx, dword[00403010] :00401057 BA63000000 mov edx, 00000063 :0040105C 891514304000 mov dword[00403014], edx :00401062 89E5 mov ebp, esp :00401064 B863000000 mov eax, 00000063 :00401069 8DB42600000000 lea esi, dword[esi+00000000] --------- :00401070 8D1401 lea edx, dword[ecx+eax] :00401073 48 dec eax :00401074 89D1 mov ecx, edx :00401076 75F8 jne 00401070 :00401078 5D pop ebp :00401079 31C0 xor eax, eax :0040107B A314304000 mov dword[00403014], eax :00401080 891510304000 mov dword[00403010], edx :00401086 C3 ret --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.135.50.147
1F:推 abcdefghi:IC Design House的考题,应该用ARM/8051 140.113.23.107 01/02 21:08
2F:→ abcdefghi:的开发工具比较好. 像ADS,GCC,GHS,Keil 140.113.23.107 01/02 21:09
3F:→ danielguo:刚试了一下 gcc, 不开最佳化有小小 220.135.50.147 01/02 21:40
4F:→ danielguo:的差异 (行数差不多, 指令不同) 220.135.50.147 01/02 21:41
※ 编辑: danielguo 来自: 220.135.50.147 (01/02 21:53)







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Soft_Job站内搜寻

TOP