现在的位置: 首页 > 综合 > 正文

Return 语句效率分析

2012年11月15日 ⁄ 综合 ⁄ 共 6437字 ⁄ 字号 评论关闭
 

不同功能的函数其内部实现各不相同,看起来似乎无法就“内部实现”达成一致的观点。但根据经验,我们可以在函数体的“入口处”和“出口处”从严把关,从而提高函数的质量。
 
l         【规则6-3-1在函数体的“入口处”,对参数的有效性进行检查。
很多程序错误是由非法参数引起的,我们应该充分理解并正确使用“断言”(assert)来防止此类错误。详见6.5节“使用断言”。
 
l         【规则6-3-2在函数体的“出口处”,对return语句的正确性和效率进行检查。
    如果函数有返回值,那么函数的“出口处”是return语句。我们不要轻视return语句。如果return语句写得不好,函数要么出错,要么效率低下。
注意事项如下:
(1)return语句不可返回指向“栈内存”的“指针”或者“引用”,因为该内存在函数体结束时被自动销毁。例如
    char * Func(void)
    {
        char str[] = “hello world”; // str的内存位于栈上
       
       return str;        // 将导致错误
    }
(2)要搞清楚返回的究竟是“值”、“指针”还是“引用”。
(3)如果函数返回值是一个对象,要考虑return语句的效率。例如   
              return String(s1 + s2);
这是临时对象的语法,表示“创建一个临时对象并返回它”。不要以为它与“先创建一个局部对象temp并返回它的结果”是等价的,如
String temp(s1 + s2);
return temp;
实质不然,上述代码将发生三件事。首先,temp对象被创建,同时完成初始化;然后拷贝构造函数把temp拷贝到保存返回值的外部存储单元中;最后,temp在函数结束时被销毁(调用析构函数)。然而“创建一个临时对象并返回它”的过程是不同的,编译器直接把临时对象创建并初始化在外部存储单元中,省去了拷贝和析构的化费,提高了效率。
类似地,我们不要将 
return int(x + y); // 创建一个临时变量并返回它
写成
int temp = x + y;
return temp;
由于内部数据类型如int,float,double的变量不存在构造函数与析构函数,虽然该“临时变量的语法”不会提高多少效率,但是程序更加简洁易读。
首先来看内部数据类型:
return int(x+y)
int temp = x+y;
return temp;
的区别:
反汇编如下:
  return   int(x+y);                       mov   eax,x  
                                             add   eax,y  
   
   
  int   temp   =   x   +   y;           mov   eax   ,x  
                                              add   eax   ,y  

                                              mov   temp,eax  
   
 
return   temp;                           mov   eax,temp  
 
可见:对于前者编译器直接将x+Y的值放如了寄存器eax中,而对于后者我们可以通过红色反汇编部分,相对前者对复制了2次(从eaxàtemp,再从tempàeax).
 
再看类类型
return string (x+y)
string temp = x+y;
return temp;
的区别:
对于前者: 编译器直接把临时对象创建并初始化在外部存储单元中,省去了拷贝和析构的化费,提高了效率
 
138:      String fun()
139:      {
0040EC70   push        ebp
0040EC71   mov         ebp,esp
0040EC73   push        0FFh
0040EC75   push        offset __ehhandler$?fun@@YA?AVString@@XZ (004132f2)
0040EC7A   mov         eax,fs:[00000000]
0040EC80   push        eax
0040EC81   mov         dword ptr fs:[0],esp
0040EC88   sub         esp,58h
0040EC8B   push        ebx
0040EC8C   push        esi
0040EC8D   push       edi
0040EC8E   lea         edi,[ebp-64h]
0040EC91   mov         ecx,16h
0040EC96   mov         eax,0CCCCCCCCh
0040EC9B   rep stos    dword ptr [edi]
0040EC9D   mov         dword ptr [ebp-1Ch],0
140:
141:         String x;
0040ECA4   push        0
0040ECA6   lea         ecx,[ebp-10h]
0040ECA9   call        @ILT+20(String::String) (00401019)
//x对象constructor
0040ECAE   mov         dword ptr [ebp-4],1
142:         String y;
0040ECB5   push        0
0040ECB7   lea         ecx,[ebp-14h]
0040ECBA   call        @ILT+20(String::String) (00401019)//y对象constructor
0040ECBF   mov         byte ptr [ebp-4],2
143:         return String(x+y);
0040ECC3   lea         eax,[ebp-14h]
0040ECC6   push        eax
0040ECC7   lea         ecx,[ebp-10h]
0040ECCA   push        ecx
0040ECCB   lea         edx,[ebp-18h]
0040ECCE   push        edx
0040ECCF   call        @ILT+25(operator+) (0040101e)//调用operator+
0040ECD4   add         esp,0Ch
0040ECD7   mov         dword ptr [ebp-20h],eax
0040ECDA   mov         eax,dword ptr [ebp-20h]
0040ECDD   mov         dword ptr [ebp-24h],eax
0040ECE0   mov         byte ptr [ebp-4],3
0040ECE4   mov         ecx,dword ptr [ebp-24h]
0040ECE7   push        ecx
0040ECE8   mov         ecx,dword ptr [ebp+8]
0040ECEB   call        @ILT+15(String::String) (00401014) //constructor临时对象
0040ECF0   mov         edx,dword ptr [ebp-1Ch]
0040ECF3   or          edx,1
0040ECF6   mov         dword ptr [ebp-1Ch],edx
0040ECF9   mov         byte ptr [ebp-4],2
0040ECFD   lea         ecx,[ebp-18h]
0040ED00   call        @ILT+5(String::~String) (0040100a)//y对象destructor
0040ED05   mov         byte ptr [ebp-4],1
0040ED09   lea         ecx,[ebp-14h]
0040ED0C   call        @ILT+5(String::~String) (0040100a)//x对象destructor
0040ED11   mov         byte ptr [ebp-4],0
0040ED15   lea         ecx,[ebp-10h]
0040ED18   call        @ILT+5(String::~String) (0040100a) //destructor  销毁临时对象
0040ED1D   mov         eax,dword ptr [ebp+8]
144:      };
 
对于后者: 上述代码将发生三件事。首先,temp对象被创建,同时完成初始化;然后拷贝构造函数把temp拷贝到保存返回值的外部存储单元中;最后,temp在函数结束时被销毁(调用析构函数)。
138:      String fun()
139:      {
0040ED80   push        ebp
0040ED81   mov         ebp,esp
0040ED83   push        0FFh
0040ED85   push        offset __ehhandler$?fun@@YA?AVString@@XZ (004132fb)
0040ED8A   mov         eax,fs:[00000000]
0040ED90   push        eax
0040ED91   mov         dword ptr fs:[0],esp
0040ED98   sub         esp,5Ch
0040ED9B   push        ebx
0040ED9C   push        esi
0040ED9D   push        edi
0040ED9E   lea         edi,[ebp-68h]
0040EDA1   mov         ecx,17h
0040EDA6   mov         eax,0CCCCCCCCh
0040EDAB   rep stos    dword ptr [edi]
0040EDAD   mov         dword ptr [ebp-20h],0
140:
141:         String x;
0040EDB4   push        0
0040EDB6   lea         ecx,[ebp-10h]
0040EDB9   call        @ILT+20(String::String) (00401019)//x对象constructor
0040EDBE   mov         dword ptr [ebp-4],1
142:         String y;
0040EDC5   push        0
0040EDC7   lea         ecx,[ebp-14h]
0040EDCA   call        @ILT+20(String::String) (00401019) //y对象constructor
 
0040EDCF   mov         byte ptr [ebp-4],2
143:         String temp;
0040EDD3   push        0
0040EDD5   lea         ecx,[ebp-18h]
0040EDD8   call        @ILT+20(String::String) (00401019)//temp对象constructor
0040EDDD   mov         byte ptr [ebp-4],3
144:         return temp=x+y;
0040EDE1   lea         eax,[ebp-14h]
0040EDE4   push        eax
0040EDE5   lea         ecx,[ebp-10h]
0040EDE8   push        ecx
0040EDE9   lea         edx,[ebp-1Ch]
0040EDEC   push        edx
0040EDED   call        @ILT+25(operator+) (0040101e)//调用operator+
0040EDF2   add         esp,0Ch
0040EDF5   mov         dword ptr [ebp-24h],eax
0040EDF8   mov         eax,dword ptr [ebp-24h]
0040EDFB   mov         dword ptr [ebp-28h],eax
0040EDFE   mov         byte ptr [ebp-4],4
0040EE02   mov         ecx,dword ptr [ebp-28h]
0040EE05   push        ecx
0040EE06   lea         ecx,[ebp-18h]
0040EE09   call        @ILT+0(String::operator=) (00401005)  //调用operator=
0040EE0E   push        eax
0040EE0F   mov         ecx,dword ptr [ebp+8]
0040EE12   call        @ILT+15(String::String) (00401014) //constructor临时对象
0040EE17   mov         edx,dword ptr [ebp-20h]
0040EE1A   or          edx,1
0040EE1D   mov         dword ptr [ebp-20h],edx
0040EE20   mov         byte ptr [ebp-4],3
0040EE24   lea         ecx,[ebp-1Ch]
0040EE27   call        @ILT+5(String::~String) (0040100a)//temp对象destructor
0040EE2C   mov         byte ptr [ebp-4],2
0040EE30   lea         ecx,[ebp-18h]
0040EE33   call        @ILT+5(String::~String) (0040100a)//y对象destructor
0040EE38   mov         byte ptr [ebp-4],1
0040EE3C   lea         ecx,[ebp-14h]
0040EE3F   call        @ILT+5(String::~String) (0040100a)//x对象destructor
0040EE44   mov         byte ptr [ebp-4],0
0040EE48   lea         ecx,[ebp-10h]
0040EE4B   call        @ILT+5(String::~String) (0040100a) //destructor销毁临时对象
0040EE50   mov         eax,dword ptr [ebp+8]
145:      };
 
可见,两中return方式都会有临时对象的产生,而不同在于String temp; retrun temp=x+y方式多了三个步骤.(即红色注释), temp对象调用constructor, 调用operator=, temp对象调用destructor.所以说return String(x+y)效率比较高
 
顺便问大虾们个问题:这段反汇编中,临时对象是放在什么地方的呀??

 

抱歉!评论已关闭.