| View previous topic :: View next topic |
| Author |
Message |
Radde Guest
|
Posted: Fri Jul 29, 2005 8:12 am Post subject: i++ or ++i efficient |
|
|
HI all,
Which is more efficient i++ or ++i and why is it??
cheers..
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Fri Jul 29, 2005 8:32 am Post subject: Re: i++ or ++i efficient |
|
|
* Radde:
| Quote: | 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?
|
|
| Back to top |
|
 |
Earl Purple Guest
|
|
| Back to top |
|
 |
red floyd Guest
|
Posted: Fri Jul 29, 2005 3:47 pm Post subject: Re: i++ or ++i efficient |
|
|
Radde wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
red floyd Guest
|
Posted: Fri Jul 29, 2005 3:51 pm Post subject: Re: i++ or ++i efficient |
|
|
red floyd wrote:
| Quote: |
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.
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Fri Jul 29, 2005 3:51 pm Post subject: Re: i++ or ++i efficient |
|
|
* red floyd:
| Quote: |
"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?
|
|
| Back to top |
|
 |
red floyd Guest
|
Posted: Fri Jul 29, 2005 3:53 pm Post subject: Re: i++ or ++i efficient |
|
|
Alf P. Steinbach wrote:
| Quote: | * 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. :)
|
|
| Back to top |
|
 |
red floyd Guest
|
Posted: Fri Jul 29, 2005 3:55 pm Post subject: Re: i++ or ++i efficient |
|
|
red floyd wrote:
| Quote: | .
Hoare's Law still applies, though.
|
Or, to make Alf happy ( ), Knuth's Law.
|
|
| Back to top |
|
 |
Marcin Kalicinski Guest
|
Posted: Fri Jul 29, 2005 5:13 pm Post subject: Re: i++ or ++i efficient |
|
|
| Quote: | 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.
|
|
| Back to top |
|
 |
Vyacheslav Kononenko Guest
|
Posted: Fri Jul 29, 2005 5:41 pm Post subject: Re: i++ or ++i efficient |
|
|
Alf P. Steinbach wrote:
| Quote: | * 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
|
|
| Back to top |
|
 |
Naif Al-Dalbahi Guest
|
Posted: Sun Jul 31, 2005 2:32 pm Post subject: Re: i++ or ++i efficient |
|
|
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
|
|
| Back to top |
|
 |
Stuart MacMartin Guest
|
Posted: Mon Aug 08, 2005 1:06 pm Post subject: Re: i++ or ++i efficient |
|
|
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
|
|
| Back to top |
|
 |
Vijai Kalyan Guest
|
Posted: Mon Aug 08, 2005 7:13 pm Post subject: Re: i++ or ++i efficient |
|
|
Well, actually, it _was_ Knuth I believe who said that.
-vijai.
|
|
| Back to top |
|
 |
red floyd Guest
|
Posted: Mon Aug 08, 2005 8:02 pm Post subject: Re: i++ or ++i efficient |
|
|
Vijai Kalyan wrote:
| Quote: | 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%2Fcomp.lang.c%2B%2B%2Fbrowse_frm%2Fthread%2F5973d9c8aab298dc%2F9c381c6ae83b014e%3Ftvc%3D1%26#doc_0009507da3eb30f4>
|
|
| Back to top |
|
 |
|