Post and pre increment

K

kailasam

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
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 
E

E. Robert Tisdale

kailasam said:
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.
 
S

Stephen L.

kailasam said:
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
 
K

kUfa

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
 
S

Stephen L.

kUfa said:
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
 
M

Malcolm

kailasam said:
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.
 
K

Krishnakumar G

E. Robert Tisdale said:
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
 
M

Mark McIntyre

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

Tom St Denis

Mark said:
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
 
J

James Kanze

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

James Kanze

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

James Kanze

|> > 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.
 
Joined
Dec 5, 2008
Messages
1
Reaction score
0
this can be a perfect answer

if u know something about the working srtucture of computer . u will definately get the answer right.
the post increment is more efficient . (means A++)

detail :

u should first know about the stack . LIFO(last in first out)

1 . suppose u give this expression to computer 5+++B. (concentrate according to "B");

LIFO means that the instruction which is last will definately perform first .

u input 5 and stored in stack (computer know this is integer)
u enter + and stored in stack (computer know this is a operator)
u enter + and stored in stack (again an operator)

now computer knows that if (++) are in stack send them to add function and add 1 and send the result back .

what u can do with the constants it will add 1 to 5 and what will happen there is nothing to store new value so increment is lost so effciency is lost .
and then it will come to "B".
u will finialize that computer is a ****ed up . he is not intellegence like us.

and if u give this operation B+++5 (concentrate according to "B")

computer first hold 5 and send it to stack .
computer will then get + and send it to stack .
again same
he know if there are "++" add the the fvalue to the first most variable.
it will add 1 in the variable value and the will add +5 in it. and gives u the output . now the computer is not confuzed .
thats all for more detail send me mail on hot,yah.
user id : zabbas44
put the server name next to my id (got it)
bye
hope u know wts going on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top