Which is more effecient ?

T

Tom St Denis

Amit said:
Which is more efficient and why?
p++ or ++p.

Open ended question. Faster in what context? What compiler? What target?

To GCC it makes no nevermind. To an older compiler without an optimizer
++p is almost always faster [specially for older 8-bit MCUs].

Tom
 
E

E. Robert Tisdale

Amit said:
Which is more efficient and why?
p++ or ++p.

It makes no difference to a good optimizing C compiler.
It could make a difference to an optimizing C++ compiler
if p is an object of a User Defined Type (UDT)
where operator++ has been overloaded.
Usually, p++ makes a copy of p, increments p
then returns the copy of the original value of p
but ++p simply increments p then returns a reference to p.
For this reason, C++ programmers try to use ++p instead of p++
even when it doesn't actually matter to the optimizing C++ compiler.
For example, C++ programmers might write

for (int j = 0; j < n; ++n) { }

instead of

for (int j = 0; j < n; n++) { }

just to help enforce a good habit.
 
M

Mark McIntyre

On 14 Jul 2003 13:49:00 -0700, in comp.lang.c ,
Which is more efficient and why?
p++ or ++p.

Since they do different things, the question is intrinsically
unanswerable:

char x[] = "hello world";
int i = 4;
int j = i;
char y = x[i++];
char z = x[++j];

Even if you're ignoring the side-effects, the answer is still
unanswerable - its implementation defined whether its faster to store
then increment, or increment then store.
 
C

Christian Bau

Which is more efficient and why?
p++ or ++p.

The short answer to this is: Write two programs, measure their execution
times. Try p += 1 and p = p + 1 as well. Can you measure any difference?
Does it matter?

The real answer is: If you worry which one is more efficient, then you
shouldn't worry about efficiency at all. You should worry about writing
code that does what it is supposed to do and that is readable, so
everyone reading the code knows that it does what it is supposed to do.

If you run into a situation where efficiency is important (for example:
You lose customers because it is too slow, or you get complaints about
the speed), then you need to learn about algorithms, profiling,
profiling, algorithms and profiling. Changing p++ to ++p won't get you
anywhere.
 
R

Richard Bos

Tom St Denis said:
Amit said:
Which is more efficient and why?
p++ or ++p.

Open ended question. Faster in what context? What compiler? What target?

To GCC it makes no nevermind. To an older compiler without an optimizer
++p is almost always faster [specially for older 8-bit MCUs].

Erm, Tom... nonsense. C is not, and never has been, as broken as C++.

Richard
 
P

Peter Shaggy Haywood

Groovy hepcat Amit was jivin' on 14 Jul 2003 13:49:00 -0700 in
comp.lang.c.
Which is more effecient ?'s a cool scene! Dig it!
Which is more efficient and why?
p++ or ++p.

What is the sound of one hand clapping?

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
H

HongKongHooker

Peter "Shaggy" Haywood said:
Groovy hepcat Amit was jivin' on 14 Jul 2003 13:49:00 -0700 in
comp.lang.c.
Which is more effecient ?'s a cool scene! Dig it!


What is the sound of one hand clapping?

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock &
roll "technically correct"?

++p would be the more efficient because p wouldn't have to be evaluated
before being incremented.

Tell me if I'm wrong guys.
 
J

Joona I Palaste

++p would be the more efficient because p wouldn't have to be evaluated
before being incremented.
Tell me if I'm wrong guys.

This would be a sensible explanation, but no one is forcing C
implementations to be sensible. In other words: It depends entirely
on how C is implemented on your platform. The standard does not say:
"This operation must be efficient", "This operation must be
inefficient", or even "This operation must be more efficient than
that operation".

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"You could take his life and..."
- Mirja Tolsa
 
R

Richard Bos

HongKongHooker said:
++p would be the more efficient because p wouldn't have to be evaluated
before being incremented.

Tell me if I'm wrong guys.

You're not only wrong, you're silly. ++p evaluates p, increases it, and
returns the new value, not necessarily in that order. p++ evaluates p,
increases it, and returns the old value, not necessarily in that order.
You tell me where the difference is.

And _please_ remember that C is not C++.

Richard
 
C

Chris Dollin

HongKongHooker wrote:

re:
++p would be the more efficient because p wouldn't have to be evaluated
before being incremented.

Tell me if I'm wrong guys.

You're wrong.

If the value of p isn't required, it doesn't have to be evaluated
before being incremented.

If the value of p *is* required, only one of p++ and ++p is correct,
so the choice wouldn't be one of efficiency. [Otherwise just use 0,
which is typically *very* efficient.]
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top