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燈, 水草

請輸入看板名稱,例如:WOW站內搜尋

TOP