• ++前置,后置++ 汇编角度效率比较


    测试环境:VS2015

    内置类型(int举例):

    1 int a=0,i=0;
    2 a = i++;
    3 a = ++i;
     1     int a=0,i=0;
     2 00921DAB  mov         dword ptr [a],0  
     3 00921DB5  mov         dword ptr [i],0  
     4     a = i++;
     5 00921DBF  mov         eax,dword ptr [i]  
     6 00921DC5  mov         dword ptr [a],eax  
     7 00921DCB  mov         ecx,dword ptr [i]  
     8 00921DD1  add         ecx,1  
     9 00921DD4  mov         dword ptr [i],ecx  
    10     a = ++i;
    11 00921DDA  mov         edx,dword ptr [i]  
    12 00921DE0  add         edx,1  
    13 00921DE3  mov         dword ptr [i],edx  
    14 00921DE9  mov         eax,dword ptr [i]  
    15 00921DEF  mov         dword ptr [a],eax 

    后置++i和i++的+1操作的执行顺序的不同,效率几乎无区别

    i++将i的值赋值到eax寄存器中,然后在将eax寄存器的值赋值到变量a所在的内存地址中,然后再讲i的值赋值到ecx,执行ecx+1,加1后再重新对i的赋值为ecx.

    ++i将i的值赋值到edx寄存器中,然后再将edx直接加1,然后赋值给i,i此时已经+1,固需要利用eax对a的内存进行赋值为i.

    非内置类型(举例):

     1 class Test
     2 {
     3 public:
     4     Test() :a(5) {}
     5     Test& operator ++()
     6     {
     7         ++a;
     8         return *this;
     9     }
    10     const Test operator++(int)
    11     {
    12         Test oldTest = *this;
    13         ++a;
    14         return oldTest;
    15     }
    16 private:
    17     int a;
    18 };
    19 int main(int argc, char *argv[])
    20 {
    21     int a=0,i=0;
    22     a = i++;
    23     a = ++i;
    24     Test test;
    25     ++test;
    26     test++;
    27 }
     1     Test test;
     2 00D81DF5  lea         ecx,[test]  
     3 00D81DFB  call        Test::Test (0D82050h)  
     4     ++test;
     5 00D81E00  lea         ecx,[test]  
     6 00D81E06  call        Test::operator++ (0D82160h)  
     7     test++;
     8 00D81E0B  push        0  
     9     test++;
    10 00D81E0D  lea         ecx,[ebp-19Ch]  
    11 00D81E13  push        ecx  
    12 00D81E14  lea         ecx,[test]  
    13 00D81E1A  call        Test::operator++ (0D82130h)  
     1     Test& operator ++()
     2     {
     3 00D82160  push        ebp  
     4 00D82161  mov         ebp,esp  
     5 00D82163  push        ecx  
     6 00D82164  mov         dword ptr [this],ecx  
     7         ++a;
     8 00D82167  mov         eax,dword ptr [this]  
     9         ++a;
    10 00D8216A  mov         ecx,dword ptr [eax]  
    11 00D8216C  add         ecx,1  
    12 00D8216F  mov         edx,dword ptr [this]  
    13 00D82172  mov         dword ptr [edx],ecx  
    14         return *this;
    15 00D82174  mov         eax,dword ptr [this]  
    16     }
    17 00D82177  mov         esp,ebp  
    18 00D82179  pop         ebp  
    19 00D8217A  ret  
     1     const Test operator++(int)
     2     {
     3 00D82130  push        ebp  
     4 00D82131  mov         ebp,esp  
     5 00D82133  sub         esp,8  
     6 00D82136  mov         dword ptr [this],ecx  
     7         Test oldTest = *this;
     8 00D82139  mov         eax,dword ptr [this]  
     9         Test oldTest = *this;
    10 00D8213C  mov         ecx,dword ptr [eax]  
    11 00D8213E  mov         dword ptr [oldTest],ecx  
    12         ++a;
    13 00D82141  mov         edx,dword ptr [this]  
    14 00D82144  mov         eax,dword ptr [edx]  
    15 00D82146  add         eax,1  
    16 00D82149  mov         ecx,dword ptr [this]  
    17 00D8214C  mov         dword ptr [ecx],eax  
    18         return oldTest;
    19 00D8214E  mov         edx,dword ptr [ebp+8]  
    20 00D82151  mov         eax,dword ptr [oldTest]  
    21 00D82154  mov         dword ptr [edx],eax  
    22 00D82156  mov         eax,dword ptr [ebp+8]  
    23     }
    24 00D82159  mov         esp,ebp  
    25 00D8215B  pop         ebp  
    26 00D8215C  ret         8  

    对于非内置类型,显然后置++要需要更多的效率开销。

  • 相关阅读:
    0430
    hlg1306再遇攻击--射线法判断点是否在多边形内部
    hlg1429凸多边形 二分+叉积
    计算几何
    像个孩子一样
    i am so happy
    hdu3371Connect the Cities---最小生成树kruskal
    hlg1339Touring DIJ+堆优化
    hdu3790最短路问题
    Lucky Goddess
  • 原文地址:https://www.cnblogs.com/Forever-Kenlen-Ja/p/7080151.html
Copyright © 2020-2023  润新知