i ++,或++ i,哪个更快? [英] i++ , or ++i, which is faster?

查看:74
本文介绍了i ++,或++ i,哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c ++标准库,教程和参考它说:


++我比i ++快。后者涉及一个临时对象因为

它必须返回i的旧值/对象。出于这个原因,使用++ i通常会更好。


但是,我认为,即使是++我仍然必须返回带有新值的临时对象是

。所以,i ++和++我必须返回一个临时的

对象。然后,i ++和++ i之间的速度差异是什么。

解决方案



< ;顾****** @ gmail.com>在消息中写道

news:11 ********************** @ g44g2000cwa.googlegr oups.com ...

c ++标准库,教程和参考书,它说:

++我比i ++快。后者涉及一个临时对象,因为它必须返回i的旧值/对象。出于这个原因,使用++ i通常会更好。

但是,我认为,即使是++我仍然需要返回一个临时对象
内有新价值。所以,i ++和++都必须返回一个临时的对象。然后,i ++和++ i之间的速度差异是什么。




前缀版本通过引用返回,因此不需要临时副本。


-Howard




< gu ****** @ gmail.com>在消息中写道

news:11 ********************** @ g44g2000cwa.googlegr oups.com ...

c ++标准库,教程和参考书,它说:

++我比i ++快。后者涉及一个临时对象,因为它必须返回i的旧值/对象。出于这个原因,使用++ i通常会更好。

但是,我认为,即使是++我仍然需要返回一个临时对象
内有新价值。所以,i ++和++都必须返回一个临时的对象。然后,i ++和++ i之间的速度差异是什么。




我还在学习C / C ++,但我刚刚完成的那一章处理过

重载运算符,包括后缀和前缀增量/减量。

根据我的理解和我做的赋值,前缀

递增/递减,++ i在你的例子中,增加了对象的值,

然后返回它。后缀增量/减量,在你的例子中是i ++,

复制对象的当前值,增加

对象的当前值,并返回副本的值。因此,前缀只有两个步骤,

其中后缀有三个。


我的解释是否正确?


戴夫


gu ****** @ gmail.com 写道:

" c ++标准库,教程和参考"书,它说:

++我比i ++快。后者涉及一个临时对象,因为它必须返回i的旧值/对象。出于这个原因,使用++ i通常会更好。

但是,我认为,即使是++我仍然需要返回一个临时对象
内有新价值。所以,i ++和++都必须返回一个临时的对象。那么,i ++和++ i之间的速度差异是什么。




不,我认为++我会返回对i的引用。 ++我返回一个新对象

包含旧的i值。对于原始类型,速度

的差异可以忽略不计。但是,如果你写了一个为每个实例分配10

兆字节内存的类,差别就会很大。


Kristo


"the c++ standard library, tutorial and reference" book, it says:

++i is faster than i++. the latter involves a temporary object because
it must return the old value/object of i. for this reason, it''s
generally better to use ++i.

but, I think, even ++i still has to return a temporary object that is
with new value inside. So, both i++ and ++i have to return a temporary
object. Then, what''s the speed difference between i++ and ++i.

解决方案


<gu******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

"the c++ standard library, tutorial and reference" book, it says:

++i is faster than i++. the latter involves a temporary object because
it must return the old value/object of i. for this reason, it''s
generally better to use ++i.

but, I think, even ++i still has to return a temporary object that is
with new value inside. So, both i++ and ++i have to return a temporary
object. Then, what''s the speed difference between i++ and ++i.



The prefix version returns by reference, so no temporary copy is needed.

-Howard



<gu******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

"the c++ standard library, tutorial and reference" book, it says:

++i is faster than i++. the latter involves a temporary object because
it must return the old value/object of i. for this reason, it''s
generally better to use ++i.

but, I think, even ++i still has to return a temporary object that is
with new value inside. So, both i++ and ++i have to return a temporary
object. Then, what''s the speed difference between i++ and ++i.



I''m still learning C/C++, but the chapter I just finished dealt with
overloading operators, including postfix and prefix increment/decrement.
According to my understanding and the assignment I did, the prefix
increment/decrement, ++i in your example, increases the value of the object,
and then returns it. The postfix increment/decrement, i++ in your example,
copies the current value of the object, increases the current value of the
object, and returns the value of the copy. Thus, prefix only has two steps,
where postfix has three.

Is my interpretation correct?

Dave


gu******@gmail.com wrote:

"the c++ standard library, tutorial and reference" book, it says:

++i is faster than i++. the latter involves a temporary object because
it must return the old value/object of i. for this reason, it''s
generally better to use ++i.

but, I think, even ++i still has to return a temporary object that is
with new value inside. So, both i++ and ++i have to return a temporary
object. Then, what''s the speed difference between i++ and ++i.



No, I think ++i returns a reference to i. ++i returns a new object
containing the old value of i. For primitive types, the speed
difference negligible. But, if you wrote a class that allocated 10
megabytes of memory for each instance, the difference would be huge.

Kristo


这篇关于i ++,或++ i,哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆