471,108 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,108 software developers and data experts.

Post and pre increment

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
Nov 14 '05 #1
13 6199
-----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-----
Nov 14 '05 #2
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.
Nov 14 '05 #3
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
Nov 14 '05 #4
> 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
Nov 14 '05 #5
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
Nov 14 '05 #6

"kailasam" <ka*********@in.bosch.com> 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.
Nov 14 '05 #7
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
Nov 14 '05 #8
In <cb**********@home.itg.ti.com> Krishnakumar G <kr*******@operamail.com> writes:
Is there any efficiency consideration here?


Nope.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #9
On Tue, 22 Jun 2004 15:23:08 +0530, in comp.lang.c , Krishnakumar G
<kr*******@operamail.com> 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 =---
Nov 14 '05 #10
Mark McIntyre wrote:
On Tue, 22 Jun 2004 15:23:08 +0530, in comp.lang.c , Krishnakumar G
<kr*******@operamail.com> 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.


Which makes me ask why someone would troll a dry group like clc. I mean I
can see sci.crypt cuz we get all the ex-cia spooks there. But clc?

No!!! My l33t C standard is the best! Just those stupid snobs at ISO won't
recognize it... so I'll write thousands of posts about my experience and
such!!! Real coders use TomSO C+++. It's the real deal.

Just doesn't quite work as well as the trolls inventing ciphers every 8
minutes in sci.crypt...

;-)

Tom
Nov 14 '05 #11
Mark McIntyre <ma**********@spamcop.net> writes:

|> On Tue, 22 Jun 2004 15:23:08 +0530, in comp.lang.c , Krishnakumar G
|> <kr*******@operamail.com> 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.

More correctly, there might be -- it depends on the hardware and the
compiler.

Theoretically, pre-incrementation will be more efficient on most
processors, for a very naïve code generator. I've never seen a code
generator that naïve, though; in every case I've seen, if the results
are not used, the compiler generates exactly the same code for both.

--
James Kanze
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Nov 14 '05 #12
"kUfa" <po*****@essi.fr> writes:

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

Which, on a PDP-11, is less efficient than post-incrementation:-).
(Unless, of course, the compiler optimizes it.)

In practice, I've never seen a compiler which didn't do this
optimization.

--
James Kanze
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Nov 14 '05 #13
"Malcolm" <ma*****@55bank.freeserve.co.uk> writes:

|> "kailasam" <ka*********@in.bosch.com> 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.

Technically, the issue is exactly the same for built-in types and for
others. Practically, it is far easier for a compiler to recognize the
optimization for a built-in type. Practically, however, most compilers
also recognize it for the overloaded operators in C++ -- the benchmarks
I've run show no difference between pre and post incrementation,
regardless of the type.

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

I suspect that the reason why i++ is more idiomatic is because it is
what Kernighan and Richie did.

--
James Kanze
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Nov 14 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Mark Turney | last post: by
15 posts views Thread by Robert Swan | last post: by
18 posts views Thread by Patrick Wood | last post: by
8 posts views Thread by Angel Tsankov | last post: by
11 posts views Thread by divya_rathore_ | last post: by
13 posts views Thread by jehugaleahsa | last post: by
3 posts views Thread by Stang1 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.