为什么前缀增量比后缀增量更快? [英] why prefix increment is faster than postfix increment?

查看:158
本文介绍了为什么前缀增量比后缀增量更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说有人说前缀增量比postfix

incerement快,但我不知道有什么区别。它们都是

i = i + 1.


i ++

++我


请指教。谢谢!!

解决方案

jr********@hotmail.com 写道:

我听说有人说前缀增量比postfix
incerement快,但我不知道有什么不同。它们都是
i = i + 1.

++ i



http://www.parashift.com/c++-faq-lit .. .html#faq-13.15


当你在那里时,请阅读整个常见问题。

Jonathan


< JR ******** @ hotmail.com>在消息中写道

news:11 ********************* @ g44g2000cwa.googlegro ups.com

我听说有人说前缀增量比postfix
incerement快,但我不知道有什么区别。他们俩都是i = i + 1。




他们显然不是都一样。前缀形式返回值增加后的变量值和后缀形式在增量之前返回变量的值




对于postfix表单,实现它的简单方法是编译器生成代码,创建一个存储原始值的临时值,然后是
然后到返回此值。对于内置类型,这是一个便宜的操作。

对于类对象,它可能是一个昂贵的操作。


-

John Carson


文章< 11 ******************* **@g44g2000cwa.googlegroups。 com>,
jr********@hotmail.com 写道:

我听说有人说前缀增量比postfix
incerement快,但我不知道有什么区别。他们俩都是i = i + 1.

我+


请指教。谢谢!!




内置运算符的C答案和C ++答案是:无论是谁b / b
使得这种说法不仅无能为力,但也是那种认为自己有线索的危险的人。不要接受

他们的_any_建议。永远。


我不认为你对操作员的C ++答案感兴趣

重载了。


I heard people saying prefix increment is faster than postfix
incerement, but I don''t know what''s the difference. They both are
i = i+1.

i++
++i

Please advise. thanks!!

解决方案

jr********@hotmail.com wrote:

I heard people saying prefix increment is faster than postfix
incerement, but I don''t know what''s the difference. They both are
i = i+1.

i++
++i



http://www.parashift.com/c++-faq-lit...html#faq-13.15

And while you''re there, read the whole faq.
Jonathan


<jr********@hotmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com

I heard people saying prefix increment is faster than postfix
incerement, but I don''t know what''s the difference. They both are
i = i+1.



They are obviously not both the same. The prefix form returns the value of
the variable after the incrementation and the postfix form returns the value
of the variable before the incrementation.

For the postfix form, the simple way to implement it is for the compiler to
produce code that creates a temporary that stores the original value and
then to return this value. For a built-in type, this is a cheap operation.
For a class object, it may be an expensive operation.

--
John Carson


In article <11*********************@g44g2000cwa.googlegroups. com>,
jr********@hotmail.com wrote:

I heard people saying prefix increment is faster than postfix
incerement, but I don''t know what''s the difference. They both are
i = i+1.

i++
++i

Please advise. thanks!!



The C answer and the C++ answer for the built-in operators are: Whoever
makes that claim is not only clueless, but is also the type of dangerous
clueless person who thinks they have a clue. Don''t take _any_ advice of
them. Ever.

I don''t think you are interested in the C++ answer for operator
overloading yet.


这篇关于为什么前缀增量比后缀增量更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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