i ++,++ i,i + = 1,i = i + 1; [英] i++, ++i, i+=1 and i = i+1;

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

问题描述

你好,

让我们说;


1)i ++; / *使用i并递增1 * /

2)++ i; / *递增i并使用它* /

3)i + = 1;

4)i = i + 1;

所有4的
结果(对于i的值)将是相同的;任何人都可以从任何角度分辨他们之间的差异吗?

我听说我们最好使用2)超过1)。并且1)比3)或4)快。


提前致谢,

Hello,
let say we have;

1) i++; /* use i and increment by one */
2) ++i; /* increment i by one and use it */
3) i += 1;
4) i = i+1;

result (for value of i) of all 4 will be same; could anyone tell
differences among them from any perspectives?
I heard we''d better use 2) over 1). And 1) is faster than 3) or 4).

Thanks in advance,

推荐答案

jim说:
jim said:

你好,

假设我们有;


1 )i ++; / *使用i并递增1 * /

2)++ i; / *递增i并使用它* /

3)i + = 1;

4)i = i + 1;

所有4的
结果(对于i的值)将是相同的;任何人都可以从任何角度分辨他们之间的差异吗?

我听说我们最好使用2)超过1)。并且1)比3)或4)快。
Hello,
let say we have;

1) i++; /* use i and increment by one */
2) ++i; /* increment i by one and use it */
3) i += 1;
4) i = i+1;

result (for value of i) of all 4 will be same; could anyone tell
differences among them from any perspectives?
I heard we''d better use 2) over 1). And 1) is faster than 3) or 4).



使用您认为最明确表达意图的那个。正如Donald Knuth所说的那样,早期优惠是所有邪恶的根源。


-

Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名, - www。

Use the one you think most clearly conveys your intent. Premature
optimisation is the root of all evil, as Donald Knuth rightly said.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


2006-12-19< e8 **** *****************@bt.com>,

Richard Heathfield写道:
2006-12-19 <e8*********************@bt.com>,
Richard Heathfield wrote:

吉姆说:
jim said:

>你好,
让我们说;

1)i ++; / *使用i并递增1 * /
2)++ i; / *递增i并使用它* /
3)i + = 1;
4)i = i + 1;

结果(对于i的值)所有4都是一样的;任何人都可以从任何角度分辨他们之间的差异吗?
我听说我们最好使用2)超过1)。并且1)比3)或4)快。
>Hello,
let say we have;

1) i++; /* use i and increment by one */
2) ++i; /* increment i by one and use it */
3) i += 1;
4) i = i+1;

result (for value of i) of all 4 will be same; could anyone tell
differences among them from any perspectives?
I heard we''d better use 2) over 1). And 1) is faster than 3) or 4).



使用您认为最明确表达意图的那个。 Donald Knuth正确地说,过早优化是所有邪恶的根源。


Use the one you think most clearly conveys your intent. Premature
optimisation is the root of all evil, as Donald Knuth rightly said.



请注意,如果在表达式中使用

,则1的含义与2 3或4略有不同。


任何有价值的编译器都会编译所有四个[在一个语句中由

自己,并在表达式中的任何地方2 3 4]完全相同

东西 - 所以,是的,使用最具可读性的东西。并且i +++ 1

而不是++ i是愚蠢的。

Note that 1 does have a meaning slightly different than 2 3 or 4, if
used in an expression.

Any compiler worth anything will compile all four [in a statement by
themselves, and 2 3 4 anywhere in an expression] to exactly the same
thing - so, yeah, use whichever one is most readable. And "i+++1"
instead of "++i" is just silly.


" jim" < ji ***** @ gmail.comwrote in message

news:11 ********************* @ t46g2000cwa.googlegro ups.com ...
"jim" <ji*****@gmail.comwrote in message
news:11*********************@t46g2000cwa.googlegro ups.com...

您好,

让我们说;


1 )i ++; / *使用i并递增1 * /

2)++ i; / *递增i并使用它* /

3)i + = 1;

4)i = i + 1;

所有4的
结果(对于i的值)将是相同的;任何人都可以从任何角度分辨他们之间的差异吗?

我听说我们最好使用2)超过1)。并且1)比3)或4)快。
Hello,
let say we have;

1) i++; /* use i and increment by one */
2) ++i; /* increment i by one and use it */
3) i += 1;
4) i = i+1;

result (for value of i) of all 4 will be same; could anyone tell
differences among them from any perspectives?
I heard we''d better use 2) over 1). And 1) is faster than 3) or 4).



如果这些行是相关表达式的全部,那么

与现代编译器完全没有区别。你可以比其他人更好地表达你的b $ b b意图,这可能有助于其他人理解你的代码,但是在编译时他们会以同样的方式结束。


过去在70年代和80年代都是如此,有些形式比其他形式更快

因为编译器不是很擅长优化,等等

建议实际上很有帮助。今天有人提出同样的建议,然而,b $ b只是传播都市传说;要小心你得到的任何东西

来自这样的来源。


S


-

Stephen Sprunk上帝不玩骰子。 --Albert Einstein

CCIE#3723上帝是一个顽固的赌徒,他会在每一个可能的机会投掷
K5SSS骰子。 --Stephen Hawking

-

通过 http://www.teranews.com

If those lines are the entirety of the relevant expressions, there is
absolutely no difference to modern compilers. One may express your
intent better than the others, which may help other folks understand
your code, but they''ll end up the same when compiled.

It used to be true, back in the 70s and 80s, that some forms were faster
than others because compilers weren''t very good at optimizing, and such
advice was actually helpful. Someone spouting the same advice today,
however, is merely propogating urban legends; beware of anything you get
from such a source.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from http://www.teranews.com


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

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