一点关于编译器不同优化级别的比较

来源:互联网 发布:php sleep 和 usleep 编辑:程序博客网 时间:2024/04/20 17:03

实验目的

在编程语言中,对比不同编程风格的代码写法,或者通过使用不同的编译器和编译优化参数,通过编译器生成汇编代码,静态分析所生成汇编代码的运行效率。

实验平台、工具

在Windows平台下,采用VC6.0工具。

 实验设计

相同编译器不同优化级别对冗余代码的优化对比。

int main()

{

int a=0; 

int b=1;

int c=2; 

int d=3;

int e=4;

c=a+b;

return c;

}



以下是无优化下生成的汇编及代码整合

 

_main PROC NEAR ; COMDAT

; 4    : {

pushebp

movebp, esp

subesp, 84; 00000054H

pushebx

pushesi

pushedi

leaedi, DWORD PTR [ebp-84]

movecx, 21; 00000015H

moveax, -858993460; ccccccccH

rep stosd

; 5    : int a=0; 

movDWORD PTR _a$[ebp], 0

; 6    : int b=1;

movDWORD PTR _b$[ebp], 1

; 7    : int c=2; 

movDWORD PTR _c$[ebp], 2

; 8    : int d=3;

movDWORD PTR _d$[ebp], 3

; 9    : int e=4;

movDWORD PTR _e$[ebp], 4

; 10   : c=a+b;

moveax, DWORD PTR _a$[ebp]

addeax, DWORD PTR _b$[ebp]

movDWORD PTR _c$[ebp], eax

; 11   : return c;

moveax, DWORD PTR _c$[ebp]

; 12   : 

; 13   : }

pop edi

pop esi

pop ebx

mov esp, ebp

pop ebp

ret 0

_main ENDP

}


以下是release优化级别的代码及汇编

 

_main PROC NEAR ; COMDAT

 

; 5    :  int a=0; 

; 6    :  int b=1;

; 7    :  int c=2; 

; 8    :  int d=3;

; 9    :  int e=4;

; 10   :  c=a+b;

; 11   :  return c;

 

moveax, 1

 

; 12   : 

; 13   : }

 

ret 0

_main ENDP





0 0
原创粉丝点击