美文网首页C++面试题集
i++与++i的效率比较

i++与++i的效率比较

作者: saviochen | 来源:发表于2017-05-12 18:51 被阅读159次

基础概念

在文章开头,先用最简洁的方式区分i++与++i:** i++表示先引用,后自增;++i则是先自增后引用。**

i++与++i的效率比较需分为以下两种情况讨论:

内建数据类型

对于内建数据类型(int\float\double等基本类型),i++与++i在单独使用时(即自增结果未被使用),效率相等。当i++与++i的结果被使用时,其效率也无差别,只不过由于自增运算的先后顺序不同,导致传值的结果不同。

下面,在vs2010中验证以上想法:

int main(){
    int i = 0;
    //00BCBB3E  mov         dword ptr [i],0 
    int x = 0;
    //00BCBB45  mov         dword ptr [x],0  
    i++;
    /*
    00BCBB4C  mov         eax,dword ptr [i]  
    00BCBB4F  add         eax,1  
    00BCBB52  mov         dword ptr [i],eax 
    */
    ++i;
    /*
    00BCBB55  mov         eax,dword ptr [i]  
    00BCBB58  add         eax,1  
    00BCBB5B  mov         dword ptr [i],eax 
    */
    x = i++;
    //先将i的值赋给x,再自增
    /*
    00BCBB5E  mov         eax,dword ptr [i]  
    00BCBB61  mov         dword ptr [x],eax  
    00BCBB64  mov         ecx,dword ptr [i]  
    00BCBB67  add         ecx,1  
    00BCBB6A  mov         dword ptr [i],ecx 
    */
    x = ++i;
    //先自增,再将i的值赋给x
    /*
    00BCBB6D  mov         eax,dword ptr [i]  
    00BCBB70  add         eax,1  
    00BCBB73  mov         dword ptr [i],eax  
    00BCBB76  mov         ecx,dword ptr [i]  
    00BCBB79  mov         dword ptr [x],ecx
    */
    return 0;
}

由上述代码可以看出,i++与++i在单独使用时被vs2010中被解释为三条汇编指令。在结果被使用的情况下,都被解释为五条汇编指令,效率相当。由此引申到,在内建数据类型的for循环中,for(int i=0; i<n; i++)for(int i=0; i<n; ++i)效率相同。

自定义数据类型

对于自定义数据类型(包括STL),前缀自增(++iterator)可以返回对象的引用,返回的结果为左值。而后缀自增(iterator++)必须返回对象的值,返回结果为临时变量,是右值。由于后缀式(iterator++)需要增加复制和析构的开销,其效率比前缀式(++iterator)低。

同样地,我们在vs2010中验证此想法:

int main(){
    vector<int> test;
    test.push_back(0);
    test.push_back(1);
    test.push_back(2);

    vector<int>::iterator iter=test.begin(), iter1;
    ++iter;
    /*
    00A9BB53  lea         ecx,[ebp-38h]  
    00A9BB56  call        std::_Vector_iterator<std::_Vector_val<int,std::allocator<int> > >::operator++ (0A8982Fh)  
    */
    iter++;
    /*
    00A9BB5B  push        0  
    00A9BB5D  lea         eax,[ebp-154h]  
    00A9BB63  push        eax  
    00A9BB64  lea         ecx,[ebp-38h]  
    00A9BB67  call        std::_Vector_iterator<std::_Vector_val<int,std::allocator<int> > >::operator++ (0A8982Ah)  
    00A9BB6C  lea         ecx,[ebp-154h]  
    00A9BB72  call        std::_Vector_iterator<std::_Vector_val<int,std::allocator<int> > >::~_Vector_iterator<std::_Vector_val<int,std::allocator<int> > > (0A894D8h)  
    */
    iter=test.begin();
    iter1 = ++iter;
    /*
    00A9BBBA  lea         ecx,[ebp-38h]  
    00A9BBBD  call        std::_Vector_iterator<std::_Vector_val<int,std::allocator<int> > >::operator++ (0A8982Fh)  
    00A9BBC2  push        eax  
    00A9BBC3  lea         ecx,[ebp-4Ch]  
    00A9BBC6  call        std::_Vector_iterator<std::_Vector_val<int,std::allocator<int> > >::operator= (0A89DBBh)  
    */
    iter1 = iter++;
    /*
    00A9BBCB  push        0  
    00A9BBCD  lea         eax,[ebp-12Ch]  
    00A9BBD3  push        eax  
    00A9BBD4  lea         ecx,[ebp-38h]  
    00A9BBD7  call        std::_Vector_iterator<std::_Vector_val<int,std::allocator<int> > >::operator++ (0A8982Ah)  
    00A9BBDC  mov         dword ptr [ebp-180h],eax  
    00A9BBE2  mov         ecx,dword ptr [ebp-180h]  
    00A9BBE8  mov         dword ptr [ebp-184h],ecx  
    00A9BBEE  mov         byte ptr [ebp-4],4  
    00A9BBF2  mov         edx,dword ptr [ebp-184h]  
    00A9BBF8  push        edx  
    00A9BBF9  lea         ecx,[ebp-4Ch]  
    00A9BBFC  call        std::_Vector_iterator<std::_Vector_val<int,std::allocator<int> > >::operator= (0A89DBBh)  
    00A9BC01  mov         byte ptr [ebp-4],2  
    00A9BC05  lea         ecx,[ebp-12Ch]  
    00A9BC0B  call        std::_Vector_iterator<std::_Vector_val<int,std::allocator<int> > >::~_Vector_iterator<std::_Vector_val<int,std::allocator<int> > > (0A894D8h)  
    */
    return 0;
}

由上述代码可以明显看出,对于自定义数据类型,无论是自增结果是否被使用,后缀自增的效率都比前缀自增低。

相关文章

  • i++与++i的效率比较

    基础概念 在文章开头,先用最简洁的方式区分i++与++i:** i++表示先引用,后自增;++i则是先自增后引用。...

  • i++与++i的区别及效率

    先看看基本区别: i++ :先在i所在的表达式中使用i的当前值,后让i加1 ++i :让i先加1,然后在i所在的表...

  • ++i 与 i++

    作者:叶王 链接:https://www.zhihu.com/question/19811087/answer/8...

  • i++与++i

    i++先操作再++ ++i先++再操作

  • i++与++i

    i++与++i对i没什么影响都是 自己加1,影响的是被赋值的元素。

  • js 中的i++与i++;

    以上的输出的结果是 0;再来看看这个

  • 自增、赋值运算符

    1.自增 int i = 3; int a = i++ + i++ + i++;//第①个i++表达是值为3,i=...

  • i++与++i——=+与+=

    i++与++i var a, i=1; a = i ++ //将i的值赋给a , 即a = i,之后再执行i = ...

  • 【C++杂记】 i++与++i 的效率问题

    今天参加面试的时候被问到了一个问题 “++i和i++在效率上有没有什么区别”,想了想除了语义上的区别,觉得应该没有...

  • 某疆笔试

    题型 选择单选多选 问答 选择 1、int i=2; int x=(i++)+(i++)+(i++);x=? x=...

网友评论

本文标题:i++与++i的效率比较

本文链接:https://www.haomeiwen.com/subject/ncqtxxtx.html