为什么++i比i++效率高?

maoliao 2002-03-27 08:36:54
为什么++i比i++效率高?
...全文
444 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
lightning 2002-03-28
  • 打赏
  • 举报
回复
to prototype
yes,,I am clear about what you talk about.i just read your answer quickly then give an answer.what you said is right
of course ,In a complex env.we should move out/in them.
but the operation that waste time mostly must be the object's construct/deconstruct.but not move in/out.I think it is more important.

NowCan 2002-03-27
  • 打赏
  • 举报
回复
我估计编译器优化后就没区别了。
coraykuang 2002-03-27
  • 打赏
  • 举报
回复
176: int i = 0;
00401C9D mov dword ptr [ebp-8],0
177: i++;
00401CA4 mov eax,dword ptr [ebp-8]
00401CA7 add eax,1
00401CAA mov dword ptr [ebp-8],eax
178: ++i;
00401CAD mov ecx,dword ptr [ebp-8]
00401CB0 add ecx,1
00401CB3 mov dword ptr [ebp-8],ecx


176: int j;
177: int i = 0;
00401C0D mov dword ptr [ebp-0Ch],0
178: j = i++;
00401C14 mov eax,dword ptr [ebp-0Ch]
00401C17 mov dword ptr [ebp-8],eax
00401C1A mov ecx,dword ptr [ebp-0Ch]
00401C1D add ecx,1
00401C20 mov dword ptr [ebp-0Ch],ecx
179: j = ++i;
00401C23 mov edx,dword ptr [ebp-0Ch]
00401C26 add edx,1
00401C29 mov dword ptr [ebp-0Ch],edx
00401C2C mov eax,dword ptr [ebp-0Ch]
00401C2F mov dword ptr [ebp-8],eax

一样的效果
prototype 2002-03-27
  • 打赏
  • 举报
回复
to lightning(lightning) :

please pay attention to my wording. all i said in my last post was 'in principle'. in practice, the temporary object 'tmp' could be optimized away. as you said, the temporary object may be just in a register. but that doesn't necessarily mean the overhead of 'i++' is the same as '++i'. it is depending on the context. stand-alone 'i++' is nearly guaranteed to be optimized to be the same as '++i'. in some other simple cases, 'i++' can also be the same as '++i'. but this is not guaranteed for all.

consider a expressin like:

func1() + func2( i++ ); // func() may be a very complex function, a register can not hold 'i''s value all the way till the end of the function call.

this could be executed as:

func1() + func2( i ); // note that at the end of this statement, 'i's value is not in the register.
i = i + 1; // computer has to read 'i's value into register and then does the addition.

compare it with the '++i' case:
func1() + func2(++i);
it will be executed like:
i = i + 1; // 'i's value is still in a register at the end of this statement.
func1() + func2( i );

so computer has to read 'i' one more time in 'i++' case than in '++i' case.

(am i missing anything here?)

oh, a note for my last post:

'int tmp = i; i=i+1; /* then use 'tmp' as 'i' in your expression.*/'

precisely, the above sentence should be written as:

'int tmp = i; /* then use 'tmp' as 'i' in your expression.*/ i=i+1;

notice 'i' is only updated after the evaluation of expression finishes. whether 'i' is still kept in a register after the evaluation is depending how complex the expression is.
hhoking 2002-03-27
  • 打赏
  • 举报
回复
有必要吗?
mis98ZB 2002-03-27
  • 打赏
  • 举报
回复
长知识了!
hcpp 2002-03-27
  • 打赏
  • 举报
回复
较同意 aileen_long(期待2002) 的说法.
maoliao 2002-03-27
  • 打赏
  • 举报
回复
gz!
lightning 2002-03-27
  • 打赏
  • 举报
回复
不同意prototype(原型)的观点,很多书上以有说明

i++,++i对内置标准类型来说起效率是一样的,其所言tmp实际是在不存在的(只是在寄存器中,++i,一样要先进,再加,再出,只不过出来时机不同)
++i比i++效率高来自重载operator++;operator++(int)后,postfix++要多一次对象构造与析构.
TalentSprite 2002-03-27
  • 打赏
  • 举报
回复
++i返回的是对象的引用,不需要调用构造函数和析构函数,而i++返回的是临时对象,要调用构造函数和析构函数。
zxy_zs 2002-03-27
  • 打赏
  • 举报
回复
如果没记错的话,++i时产生的函数调用是class1.operator++();而i++时产生的函数调用是class1.operator++(int),会多生成一个0的参数来区分前置或后置吧。
QSQ99 2002-03-27
  • 打赏
  • 举报
回复
看看谭浩强的 C语言程序设计
maoliao 2002-03-27
  • 打赏
  • 举报
回复
Thank you, prototype and aileen_long.
aileen_long 2002-03-27
  • 打赏
  • 举报
回复
这并不是绝对的,我个人认为,就C++的内置类型来说,++i和i++没有什么区别。之所以很多书上提倡前置的自增,也许是希望编程人员养成一种良好的习惯。因为对于自定义类型,如果重载这两种形式的自增操作,那么良好的设计是,先实现前置自增,而在后置自增的实现里,只需要加一句保存自增前的值的代码,以便返回,剩下的只需要调用前置自增的操作即可,也就是它不用再写任何自增的代码。这样的一个最大优点就是,如果今后需要对自增操作进行修改,那么只需要修改一个函数的代码即可。所以这两种自增操作的效率哪一种更高,自然不言而喻.
ciml 2002-03-27
  • 打赏
  • 举报
回复
对,i++要产生一个临时变量!
prototype 2002-03-27
  • 打赏
  • 举报
回复
in principle,

'++i' is equivalent to: 'i=i+1; /* then use 'i' as the 'i' in your expression. */'
'i++' is equivalent to: 'int tmp = i; i=i+1; /* then use 'tmp' as 'i' in your expression. */'

so '++i' is a bit more efficient than 'i++'. however, if you just write a stand-along 'i++' or '++i', the temporary object 'tmp' is unnecessary, so any compiler should be able to completely optimize that way, making 'i++' is equally efficient as '++i'.

i_jianyong 2002-03-27
  • 打赏
  • 举报
回复
我真菜,第一次听说。我说Lippman怎么老用++i呢。以后不用i++了。
duanfeng 2002-03-27
  • 打赏
  • 举报
回复
好象 more effective C++ 有讲
pilishine 2002-03-27
  • 打赏
  • 举报
回复
这跟编译器的设计有关系
zjlsct 2002-03-27
  • 打赏
  • 举报
回复
学习中……

69,375

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧