i++ and ++i

J

jsshah

I know that one of them is efficient. But dont know which one of them.
Can someone explain me which one of them is efficient and why?

Thanks in advance.
 
L

Lew Pitcher

jsshah said:
I know that one of them is efficient.

Then you know wrong. Neither (or both, for that matter) are effecient.
But dont know which one of them.
Can someone explain me which one of them is efficient and why?

Nope. No one can explain to you which one is effecient.

Someone here will, no doubt, explain to you why you have asked a
nonsense (wrt C) question. I hope you read their answer.
 
C

Christopher Benson-Manica

(WRT ++i vs. i++ being "efficient")
Then you know wrong. Neither (or both, for that matter) are effecient.
Someone here will, no doubt, explain to you why you have asked a
nonsense (wrt C) question. I hope you read their answer.

I think this is unnecessarily harsh on OP, who seems to have simply
neglected to include the word "more" in his or her query. I think the
"it depends on your implementation" answer would have been more
appropriate, or at the least more helpful and informative. Then
again, perhaps the "STFW" answer would have been ideal, that is both
snarky and potentially helpful.
 
M

Mark Odell

jsshah said:
I know that one of them is efficient. But dont know which one of them.
Can someone explain me which one of them is efficient and why?

I suppose it's possible for an implementation to execute one form more
efficiently than the other. You would need to look at the assembler
output for the two cases. One might image that it *could* be easier for
the compiler to generate fewer instructions for ++i than i++ but I'd be
surprised if modern compilers couldn't detect either idiom and do the
smart thing in both cases.
 
D

Dave Dunfield

I know that one of them is efficient. But dont know which one of them.
I suppose it's possible for an implementation to execute one form more
efficiently than the other. You would need to look at the assembler
output for the two cases.

C mandates nothing about the "efficiency" of implementation, therefore
in a given C implementation, either operation may result in anything from
a single increment instruction for a given usage to a call to multiple
nested subroutines (unlikely but possble). And this could change with the
exact usage. - A different implementation may do the reverse, generate
one code sequence for one form, and a completely different code sequence
for another form. Examining the assembly output of a compiler may give you
a hint as to which is more efficient for that particular compiler and usage,
however it will tell you nothing about the general nature of the operations
(ie: any results you get will be highly compiler/platform/use dependant).

As most of us know (but just in case the OP doesn't) , these are not
the same operator:

++i evaluates to (i+1) with the side effect of incrementing i (in other
words - returns the value of I AFTER the increment)

i++ evaluates to (i) with the side effect of incrementing i (in other
words - return the value of I BEFORE the increment)

One may be more "efficient" than the other in a particular algorithm,
(for example using ++i in a case where you need the preincremented
value means either using (i-1) later, or another variable) - however
even in this case, most compilers will optimize the results to the same
or equivalent code - And who knows - some perverse architecture
could exist where the opposite of "what you expect" in terms of
efficiency is true. The point I am making is that you really cannot
make a correlation between individual C operations (or even groups
of operations) and efficiency. Write clean readable straightforward
code and trust the compiler to do it's job.

One might image that it *could* be easier for
the compiler to generate fewer instructions for ++i than i++ but I'd be
surprised if modern compilers couldn't detect either idiom and do the
smart thing in both cases.

This caught my eye (and is the main reason I responded) ... In very
early versions of my compiler, for certain register limited architectures,
i++ could generate
load i
increment
store i
decrement

And the compiler wasn't bright enough to recognize those operations
which were being performed for the side effect only, resulting in
extra an unneeded "decrement" operations when i++ was used
independantly, but not when ++i was used.

This artifact is long gone, and both operations will produce exactly the
same code in all architectures I support now - but it does provide a
real example of how one CAN be more efficient that the other IN A
GIVEN IMPLEMENTATION. The problem is there are no rules that
you can use to determine this.

When you are using these operations for the side effect only, it really
comes down to a matter of personal taste/style. Partly because of the
behaviour of my early compilers described above, but mostly because
I think "increment i" and not "i gets incremented", I code ++i instead of
i++ ... but I would not expect either to consistantly produce more efficient
(or even different) code than the other.

Regards,
 
A

Andrew Poelstra

(WRT ++i vs. i++ being "efficient")



I think this is unnecessarily harsh on OP, who seems to have simply
neglected to include the word "more" in his or her query. I think the
"it depends on your implementation" answer would have been more
appropriate, or at the least more helpful and informative. Then
again, perhaps the "STFW" answer would have been ideal, that is both
snarky and potentially helpful.

Uhm, I can't think of an implementation where the two of those would
have different opcode sequences, let alone a difference in speed. It
can be said with 99.99995% certainty that neither of them is faster
than the other by a meaningful amount.
 
I

Ian Collins

jsshah said:
I know that one of them is efficient. But dont know which one of them.
Can someone explain me which one of them is efficient and why?
Maybe you are thinking of C++ where pre-increment is generally faster
for user defined types. For PODs there probably isn't any difference.
 
K

Keith Thompson

Andrew Poelstra said:
Uhm, I can't think of an implementation where the two of those would
have different opcode sequences, let alone a difference in speed. It
can be said with 99.99995% certainty that neither of them is faster
than the other by a meaningful amount.

I think you're assuming that they're being used in a statement
context. For example, I'd (almost) certainly expect identical code
for these two statements:

i++;
++i;

But if they're used in a larger expression:

j = i++;
j = ++i;

then they mean different things, and of course you'll get different
code for them. (Which is more efficient is a different question, and
one that the language doesn't answer.)
 
F

Frederick Gotham

jsshah posted:
I know that one of them is efficient. But dont know which one of them.
Can someone explain me which one of them is efficient and why?


The C programming language is intended for writing programs for a wide
variety of systems.

These different systems do things differently internally, and so some
things may be less or more efficient on different systems.

A good example would be the portable way to copy a string:

Example (1)

void CpyStr(char *dest,char const *source)
{
for ( ; *dest++ = *source++; );
}

Example (2)

void CpyStr(char *dest,char const *source)
{
while ( ; *dest = *source; ++dest,++source);
}

At first glance, Example (2) would appear to be more efficient, as it
doesn't perform an extra redundant incrementation. However, consider if
you're working on a system where the following two activities can be
performed in one sole CPU instruction:

(1) Retrieve the data pointed at by a pointer.
(2) Increment the pointer.

On such a system, Example (1) would certainly be more efficient, even
though it performs an extra redundant incrementation. (There are such
systems which can do this in one instruction).

As for your question of "i++" Vs. "++i", well I've a couple of things to
say:

(1) Either might be more efficient, depending on the system.
(2) When your compiler sees the expression on its own, and realises that
the value of the expression is discarded, then it may simply use the more
efficient one.
(3) I myself favour pre-incrementation wherever possible, i.e. "++i", as
it's the most simple one to get the job done, and its functionality
coincides with that of the other operators such as "=", "+=", "-=", "&=".

You'll see many people use "i++" where "++i" would suffice. It's just their
style. I have my own style, and I use "++i".
 
C

Chris Dollin

Frederick said:
jsshah posted:



The C programming language is intended for writing programs for a wide
variety of systems.

These different systems do things differently internally, and so some
things may be less or more efficient on different systems.

A good example would be the portable way to copy a string:

Example (1)

void CpyStr(char *dest,char const *source)
{
for ( ; *dest++ = *source++; );
}

Example (2)

void CpyStr(char *dest,char const *source)
{
while ( ; *dest = *source; ++dest,++source);
}

At first glance, Example (2) would appear to be more efficient, as it
doesn't perform an extra redundant incrementation. However, consider if
you're working on a system where the following two activities can be
performed in one sole CPU instruction:

(1) Retrieve the data pointed at by a pointer.
(2) Increment the pointer.

On such a system, Example (1) would certainly be more efficient,

That depends on the compiler: I can see that a compiler might compile (2)
as though it were (1), or as peephole optimisation transform

ldr r0, [r1]
add r1, r1, #1
into
ldr r0, [r1, #1]!
even
though it performs an extra redundant incrementation. (There are such
systems which can do this in one instruction).

As for your question of "i++" Vs. "++i", well I've a couple of things to
say:

(1) Either might be more efficient, depending on the system.
(2) When your compiler sees the expression on its own, and realises that
the value of the expression is discarded, then it may simply use the more
efficient one.
(3) I myself favour pre-incrementation wherever possible, i.e. "++i", as
it's the most simple one to get the job done,

? It's as simple as i++, surely.
and its functionality
coincides with that of the other operators such as "=", "+=", "-=", "&=".

You'll see many people use "i++" where "++i" would suffice.

"Suffice" suggests that i++ does something ++i doesn't.
It's just their
style. I have my own style, and I use "++i".

My style twitch is using `i += 1` when the result will be discarded.
 
J

John Bode

jsshah said:
I know that one of them is efficient. But dont know which one of them.
Can someone explain me which one of them is efficient and why?

Thanks in advance.

As standalone expression in statements like

i++;
for(i = 0; i < MAX; ++i) ...
while(--i) ...
if (i++) ...

the two forms should result in the same operations (and therefore be
equally efficient). As parts of larger expressions, they may not
result in the same operations, but those differences shouldn't be
significant over a wide range of expressions.

Finally, it doesn't really matter which is more efficient, because
you're ultimately going to use the form that gives the result you need.
For example, think of a simple array-based stack:

#define STACK_SIZE ...
static int sp = STACK_SIZE; // stack pointer
static int stack[STACK_SIZE];
...
void push(int val)
{
if (sp)
stack[--sp] = val; // subtract 1 from sp, save val to this
location
else
/* stack overflow error */
}

int pop(void)
{
if (sp < STACK_SIZE)
return stack[sp++]; // return val from stack[sp], add one
to sp
else
/* stack underflow error */
}

The choice of the prefix or postfix form is driven by how the algorithm
works, not by which one is more efficient.
 
A

Andrew Poelstra

I think you're assuming that they're being used in a statement
context. For example, I'd (almost) certainly expect identical code
for these two statements:

i++;
++i;

But if they're used in a larger expression:

j = i++;
j = ++i;

then they mean different things, and of course you'll get different
code for them. (Which is more efficient is a different question, and
one that the language doesn't answer.)

1) Technically, they both increment i and store i in j, although they
do so in different ordered. I can think of contexts where one might
lend itself to additional optimization, but otherwise I can't think
of a situation where they'd be compiled differently.
2) Even if one is faster, the chances of it being a worthy optimization
(as opposed to a style decision) are very slim.

Those two, and this is getting pretty OT.
 
S

Stephen Sprunk

jsshah said:
I know that one of them is efficient. But dont know which one of
them.
Can someone explain me which one of them is efficient and why?

C itself makes no claim about efficiency; that's an implementation
detail.

On ancient compilers running on ancient computers, ++i was often
slightly faster than i++. However, with a modern optimizing compiler
on a modern OoO machine, there should be no measurable difference
between the two -- and what little difference there may be will vary
depending on context.

S
 
K

Keith Thompson

John Bode said:
As standalone expression in statements like

i++;
for(i = 0; i < MAX; ++i) ...
while(--i) ...
if (i++) ...

the two forms should result in the same operations (and therefore be
equally efficient). As parts of larger expressions, they may not
result in the same operations, but those differences shouldn't be
significant over a wide range of expressions.

Your last two examples aren't standalone expressions. The result of
the expression is used as a condition, so there's a semantic
difference between the prefix and postfix forms.
 

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

Forum statistics

Threads
474,184
Messages
2,570,979
Members
47,579
Latest member
CharaS3188

Latest Threads

Top