Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Post and pre increment

Reply
Thread Tools

Post and pre increment

 
 
kailasam
Guest
Posts: n/a
 
      06-14-2004
Hello,

Iam having a doubt. Which is more efficient post increment or Pre increment?
I have read that preincrement is efficient than Post increment. Iam not able
to think how it is?
For an independent statement i++ and ++i which is more efficient?

Regards

Kailasam


 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      06-14-2004
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

kailasam wrote:
> Hello,
>
> Iam having a doubt. Which is more efficient post increment or Pre increment?


Neither. As far as C is concerned, both are equally effecient.
As far as the underlying machine language is concerned, it depends on the
processor and the compiler. But that is off-topic in comp.lang.c




- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFAzTGYagVFX4UWr64RAtj2AKDPPiDseqeVwb/9+XBi7Y8ZOusAOACfWXYA
5fEPwFN0R7FB0Lx6dXccgf8=
=MhJC
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      06-14-2004
kailasam wrote:

> Which is more efficient post increment or Pre increment?
> I have read that preincrement is efficient than Post increment.
> I am not able to think how it is?
> For an independent statement i++ and ++i which is more efficient?


This is only important in C++
where increment operators have been overloaded for *large* objects.
If you write C++ code as well as C code.
it is probably a good idea to use the preincrement operator

++i;

instead of the post increment operator.
 
Reply With Quote
 
Stephen L.
Guest
Posts: n/a
 
      06-14-2004
kailasam wrote:
>
> Hello,
>
> Iam having a doubt. Which is more efficient post increment or Pre increment?
> I have read that preincrement is efficient than Post increment. Iam not able
> to think how it is?
> For an independent statement i++ and ++i which is more efficient?
>
> Regards
>
> Kailasam


If you're not interested in its side effect(s),
then use whatever fits the readability/style
of the code you're working in. An optimizing
compiler _should_ choose the most efficient
way to implement the operation (you could run
benchmarks if necessary to see for yourself).

-
Stephen
 
Reply With Quote
 
kUfa
Guest
Posts: n/a
 
      06-14-2004
> If you're not interested in its side effect(s),
> then use whatever fits the readability/style
> of the code you're working in. An optimizing
> compiler _should_ choose the most efficient
> way to implement the operation (you could run
> benchmarks if necessary to see for yourself).


Well looking the output asm source code might be easier, imo. I personnaly
prefer not relying on any compiler asumption and use use the preincrement
operator.

/David


 
Reply With Quote
 
Stephen L.
Guest
Posts: n/a
 
      06-14-2004
kUfa wrote:
>
> > If you're not interested in its side effect(s),
> > then use whatever fits the readability/style
> > of the code you're working in. An optimizing
> > compiler _should_ choose the most efficient
> > way to implement the operation (you could run
> > benchmarks if necessary to see for yourself).

>
> Well looking the output asm source code might be easier, imo. I personnaly
> prefer not relying on any compiler asumption and use use the preincrement
> operator.
>
> /David




Some compilers don't emit debugging records with
optimization turned on. This makes it tedious
to find the _exact_ instructions for the C operation
in question in the ASM output. Also, with the
modern CPU architectures, the compiler may be taking
advantage of parallel execution of ASM instructions
which look inefficient to the casual observer.


-
Stephen
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      06-14-2004

"kailasam" <(E-Mail Removed)> wrote
> Iam having a doubt. Which is more efficient post increment or
> Pre increment?
> I have read that preincrement is efficient than Post increment.
> Iam not able to think how it is?
>

You have read about C++. C++ allows overloading of the ++ operator, but the
form x++ forces the "operator ++" function to return a temporary copy of x
to maintain the function semantics, whilst the pre-increment ++ form doesn't
have this problem. For built-in types this not an issue.
>
> For an independent statement i++ and ++i which is more
> efficient?
>

Almost certainly both the same, and if there is a difference it would only
be a cycle or two. However in compound statements you generally need the
post increment form, because the pointer you are passed or initially
calculate points to the first object.

for(i=0;i<N;i++)
*ptr++ = x;

This means that it is probably more idiomatic to write "i++" in the for loop
rather than "++i", though both are essentially equivalent.


 
Reply With Quote
 
Krishnakumar G
Guest
Posts: n/a
 
      06-22-2004
E. Robert Tisdale wrote:
> kailasam wrote:
>
>> Which is more efficient post increment or Pre increment?
>> I have read that preincrement is efficient than Post increment.
>> I am not able to think how it is?
>> For an independent statement i++ and ++i which is more efficient?

>
>
> This is only important in C++
> where increment operators have been overloaded for *large* objects.
> If you write C++ code as well as C code.
> it is probably a good idea to use the preincrement operator

Is there any efficiency consideration here?

>
> ++i;
>
> instead of the post increment operator.


Regards,
Krishna
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      06-22-2004
In <cb8vi5$l7a$(E-Mail Removed)> Krishnakumar G <(E-Mail Removed)> writes:

>Is there any efficiency consideration here?


Nope.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      06-22-2004
On Tue, 22 Jun 2004 15:23:08 +0530, in comp.lang.c , Krishnakumar G
<(E-Mail Removed)> wrote:

>E. Robert Tisdale wrote:
>> If you write C++ code as well as C code.
>> it is probably a good idea to use the preincrement operator


>Is there any efficiency consideration here?


Not in C. If you're asking about C++, thats down the hall.

For future reference ERT is a well-known troll. Consider all his advice as
suspicious until its confirmed by a regular poster here.

By the way undoubtedly trollsdale will reply to this post by accusing me of
being a troll. This is his standard technique.

--
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 =---


----== 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 =---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Post increment ++ has higher precedence than pre increment ++. Why? Alf P. Steinbach /Usenet C++ 0 05-22-2011 12:03 PM
post increment or pre increment? Peng Yu Perl Misc 7 11-23-2008 11:44 PM
post and pre-increment operator overloading not behaving like simudream@gmail.com C++ 8 11-22-2007 10:20 AM
Can someone tell me why? (Unary pre and post increment operator Andreas Sheriff C++ 10 09-25-2004 02:33 AM
pre, post increment standard behaviour, and friend function declaration eddiew_AUS C++ 18 01-27-2004 04:31 PM



Advertisments