By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,908 Members | 1,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,908 IT Pros & Developers. It's quick & easy.

i++ or ++i efficient

P: n/a
HI all,
Which is more efficient i++ or ++i and why is it??
cheers..

Jul 29 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
* Radde:
Which is more efficient i++ or ++i and why is it??


The FAQ is at <url: http://www.parashift.com/c++-faq-lite/>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 29 '05 #2

P: n/a

Alf P. Steinbach wrote:
* Radde:
Which is more efficient i++ or ++i and why is it??


The FAQ is at <url: http://www.parashift.com/c++-faq-lite/>.

and this is the section you want:

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

Jul 29 '05 #3

P: n/a
Radde wrote:
HI all,
Which is more efficient i++ or ++i and why is it??


If you have to ask the question "why", then you don't understand the
language enough to need to worry about it.

"Premature optimization is the root of all evil." -- Hoare's Law
Jul 29 '05 #4

P: n/a
* red floyd:

"Premature optimization is the root of all evil." -- Hoare's Law


When did Hoare grab the honor of being the one who said that, from Knuth?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 29 '05 #5

P: n/a
red floyd wrote:

If you have to ask the question "why", then you don't understand the
language enough to need to worry about it.

"Premature optimization is the root of all evil." -- Hoare's Law


To reply to my own post. Sorry, that sounded snarky.

What I meant was: to worry about such things without seeing if its
actually an issue is premature and irrelevant. If your code is so tight
that the few microseconds difference between ++i and i++ is critical,
then you should be understanding the language anyways.

The difference between ++i and i++ is explained in any introductory C++
(or C) book.

Note, the comment about a few microsecods applies unless you're dealing
with user-defined objects, in which case you should *already* know the
difference, having written both operators.

Hence the snarky comment.

Hoare's Law still applies, though.
Jul 29 '05 #6

P: n/a
Alf P. Steinbach wrote:
* red floyd:
"Premature optimization is the root of all evil." -- Hoare's Law

When did Hoare grab the honor of being the one who said that, from Knuth?


I had always heard it was Hoare. Maybe it was Knuth. What do I know?
I'm just a hack. :) It's still true. :)

Jul 29 '05 #7

P: n/a
red floyd wrote:
.

Hoare's Law still applies, though.


Or, to make Alf happy (:)), Knuth's Law.
Jul 29 '05 #8

P: n/a
> What I meant was: to worry about such things without seeing if its
actually an issue is premature and irrelevant. If your code is so tight
that the few microseconds difference between ++i and i++ is critical, then
you should be understanding the language anyways.


There will be not even a nanosecond difference, because the code generated
for both cases is exactly the same for built-in types on all sensible
compilers (unless return value is used of course).

cheers,
M.
Jul 29 '05 #9

P: n/a

Alf P. Steinbach wrote:
* red floyd:

"Premature optimization is the root of all evil." -- Hoare's Law


When did Hoare grab the honor of being the one who said that, from Knuth?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

From: http://en.wikipedia.org/wiki/Talk:C._A._R._Hoare

The quotation has been attributed to Knuth because it was written by
Knuth. It appears in his article Structured Programming with GO TO
Statements that appeared in ACM Computing Surveys, Vol. 6, No. 4,
December 1974, p. 268. A more complete version is:

There is no doubt that the grail of efficiency leads to abuse.
Programmers waste enormous amounts of time thinking about, or worrying
about, the speed of noncritical parts of their programs, and these
attempts at efficiency actually have a strong negative impact when
debugging and maintenance are considered. We should forget about small
efficiencies, say about 97% of the time: premature optimization is the
root of all evil.
-- Dominus 14:27, 3 Dec 2004 (UTC)

On further investigation, I find that Knuth does attribute the
"premature optimization" aphorism to Hoare. (Although not, obviously,
the particular statement of it quoted above.) See Knuth's 1989 paper
The Errors of TeX, section F:

(But I also knew, and forgot, Hoare's dictum that premature
optimization is the root of all evil in programming.)
-- Dominus 02:03, 4 Dec 2004 (UTC)

Regards,
Vyacheslav

Jul 29 '05 #10

P: n/a
when it comes to efficiency , both expression they have the same level


but ++i means , increment i first then use it
and i++ use the current value of i then increment it

Jul 31 '05 #11

P: n/a
Beg to differ with other posters, but this depends on what "i" is. If
it's a complex object, then ++i can be significantly faster than i++.

Stuart

Aug 8 '05 #12

P: n/a
Well, actually, it _was_ Knuth I believe who said that.

-vijai.

Aug 8 '05 #13

P: n/a
Vijai Kalyan wrote:
Well, actually, it _was_ Knuth I believe who said that.

-vijai.


See Vyacheslav Kononenko's response to that:

<http://groups-beta.google.com/group/comp.lang.c++/tree/browse_frm/thread/5973d9c8aab298dc/9c381c6ae83b014e?rnum=1&hl=en&_done=%2Fgroup%2Fcom p.lang.c%2B%2B%2Fbrowse_frm%2Fthread%2F5973d9c8aab 298dc%2F9c381c6ae83b014e%3Ftvc%3D1%26#doc_0009507d a3eb30f4>
Aug 8 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.