哪个更有效? [英] Which is more effecient ?
问题描述
哪个更有效率,为什么?
p ++或++ p。
谢谢。
Which is more efficient and why?
p++ or ++p.
Thanks.
推荐答案
Amit写道:
哪个更有效率,为什么?
p ++或++ p。
Which is more efficient and why?
p++ or ++p.
开放式问题。在什么情况下更快?什么编译器?目标是什么?
对于海湾合作委员会而言,它不会让人失望。对于没有优化器的旧编译器
++ p几乎总是更快[特别适用于较旧的8位MCU]。
Tom
Open ended question. Faster in what context? What compiler? What target?
To GCC it makes no nevermind. To an older compiler without an optimizer
++p is almost always faster [specially for older 8-bit MCUs].
Tom
2003年7月14日13:49:00 -0700,在comp.lang.c中,
vi ******************* @ yahoo.com (阿米特)写道:
On 14 Jul 2003 13:49:00 -0700, in comp.lang.c ,
vi*******************@yahoo.com (Amit) wrote:
哪个更有效,为什么?
p ++或++ p。
Which is more efficient and why?
p++ or ++p.
因为他们做不同的事情,问题本质上是
无法回答:
char x [] =" hello world" ;;
int i = 4;
int j = i;
char y = x [i ++];
char z = x [++ j] ;
即使你忽略了副作用,答案仍然是无法回答的 - 它的实现定义了它是否更快存储
然后增加,或增加然后存储。
-
马克麦金太尔
CLC常见问题< http:// www .eskimo.com /〜考察/ C -faq / top.html>
CLC自述文件:< http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
---- ==通过Newsfeed.Com发布 - 无限制 - 未经审查 - 安全Usenet新闻== ----
http://www.newsfeed.com 世界排名第一的新闻组服务! > 100,000新闻组
--- = 19东/西海岸专业服务器 - 通过加密的总隐私= ---
Since they do different things, the question is intrinsically
unanswerable:
char x[] = "hello world";
int i = 4;
int j = i;
char y = x[i++];
char z = x[++j];
Even if you''re ignoring the side-effects, the answer is still
unanswerable - its implementation defined whether its faster to store
then increment, or increment then store.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Amit写道:
Amit wrote:
哪个更有效率,为什么?
p ++或++ p。
Which is more efficient and why?
p++ or ++p.
没什么区别优化C编译器。
如果p是用户定义类型(UDT)的对象,它可能会对优化的C ++编译器产生影响
其中operator ++已经过载。
通常,p ++复制p,递增p
然后返回原始值的副本p >
但++ p只是递增p然后返回对p的引用。
因此,C ++程序员尝试使用++ p而不是p ++
即使它对优化的C ++编译器并不重要。
例如,C ++程序员可能会为(int j = 0; j)编写
< n; ++ n){}
而不是
for(int j = 0; j< N; n ++){}
只是为了帮助改善一个好习惯。
It makes no difference to a good optimizing C compiler.
It could make a difference to an optimizing C++ compiler
if p is an object of a User Defined Type (UDT)
where operator++ has been overloaded.
Usually, p++ makes a copy of p, increments p
then returns the copy of the original value of p
but ++p simply increments p then returns a reference to p.
For this reason, C++ programmers try to use ++p instead of p++
even when it doesn''t actually matter to the optimizing C++ compiler.
For example, C++ programmers might write
for (int j = 0; j < n; ++n) { }
instead of
for (int j = 0; j < n; n++) { }
just to help enforce a good habit.
这篇关于哪个更有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!