473,378 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 6334
-----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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a...
15
by: Robert Swan | last post by:
I'd like to know why the following program outputs 1, and not 0. #include <iostream> class a { int v; public: a():v(0){} a& operator++(int) { v++;
18
by: Patrick Wood | last post by:
I found a problem with C# and post increments. I was going through some source code in c++ and found someone did a post increment: int x=0; for(int i=0; i<10; i++) { x = x++; }
8
by: Angel Tsankov | last post by:
Should pre/post increment/decrement return const or non-const? What about other functions?
1
by: mohsin | last post by:
hi everyone well i m little bit confused about the use of pre an post increment infact my thinking contradict with the logic of programe lets consider an example x=5; y=x++; z=x; after the...
11
by: divya_rathore_ | last post by:
The code: int aaa = 100; printf("%d %d %d\n", --aaa, aaa, aaa--); printf("%d\n", aaa); prints: 99 100 100 98
13
by: jehugaleahsa | last post by:
Hello: In C++, you had to distinguish between post and pre increments when overloading. Could someone give me a short demonstration of how to write these? I get the impression that are...
5
by: simudream | last post by:
//hi maybe helpful others can look at this code and //tell me why the class code won't behave like //intrinsic types with post and pre increment //Version: 1.00 #include <iostream> using...
3
by: Stang1 | last post by:
The following statement: line_buf = ' '; is equivalent to: line_buf = ' '; line_len++;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.