C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

i++ or ++i efficient

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Radde
Guest





PostPosted: Fri Jul 29, 2005 8:12 am    Post subject: i++ or ++i efficient Reply with quote



HI all,
Which is more efficient i++ or ++i and why is it??


cheers..

Back to top
Alf P. Steinbach
Guest





PostPosted: Fri Jul 29, 2005 8:32 am    Post subject: Re: i++ or ++i efficient Reply with quote



* 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





PostPosted: Fri Jul 29, 2005 11:43 am    Post subject: Re: i++ or ++i efficient Reply with quote




Alf P. Steinbach wrote:
Quote:
* 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-lite/operator-overloading.html#faq-13.15
/>


Back to top
red floyd
Guest





PostPosted: Fri Jul 29, 2005 3:47 pm    Post subject: Re: i++ or ++i efficient Reply with quote

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





PostPosted: Fri Jul 29, 2005 3:51 pm    Post subject: Re: i++ or ++i efficient Reply with quote

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





PostPosted: Fri Jul 29, 2005 3:51 pm    Post subject: Re: i++ or ++i efficient Reply with quote

* 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





PostPosted: Fri Jul 29, 2005 3:53 pm    Post subject: Re: i++ or ++i efficient Reply with quote

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. Smile It's still true. :)


Back to top
red floyd
Guest





PostPosted: Fri Jul 29, 2005 3:55 pm    Post subject: Re: i++ or ++i efficient Reply with quote

red floyd wrote:
Quote:
.

Hoare's Law still applies, though.

Or, to make Alf happy (Smile), Knuth's Law.

Back to top
Marcin Kalicinski
Guest





PostPosted: Fri Jul 29, 2005 5:13 pm    Post subject: Re: i++ or ++i efficient Reply with quote

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





PostPosted: Fri Jul 29, 2005 5:41 pm    Post subject: Re: i++ or ++i efficient Reply with quote


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





PostPosted: Sun Jul 31, 2005 2:32 pm    Post subject: Re: i++ or ++i efficient Reply with quote

when it comes to efficiency , both expression they have the same level
Quote:


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





PostPosted: Mon Aug 08, 2005 1:06 pm    Post subject: Re: i++ or ++i efficient Reply with quote

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





PostPosted: Mon Aug 08, 2005 7:13 pm    Post subject: Re: i++ or ++i efficient Reply with quote

Well, actually, it _was_ Knuth I believe who said that.

-vijai.

Back to top
red floyd
Guest





PostPosted: Mon Aug 08, 2005 8:02 pm    Post subject: Re: i++ or ++i efficient Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.