Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Which is more effecient ?

Reply
Thread Tools

Which is more effecient ?

 
 
Amit
Guest
Posts: n/a
 
      07-14-2003
Which is more efficient and why?
p++ or ++p.

Thanks.
 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      07-14-2003
Amit wrote:
> 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

 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      07-14-2003
Amit wrote:

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



 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      07-14-2003
On 14 Jul 2003 13:49:00 -0700, in comp.lang.c ,
http://www.velocityreviews.com/forums/(E-Mail Removed) (Amit) wrote:

>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.
--
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 =---
 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      07-14-2003
In article <(E-Mail Removed)> ,
(E-Mail Removed) (Amit) wrote:

> 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.
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      07-15-2003
Tom St Denis <(E-Mail Removed)> wrote:

> Amit wrote:
> > 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
 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      07-18-2003
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"?
 
Reply With Quote
 
HongKongHooker
Guest
Posts: n/a
 
      07-18-2003

"Peter "Shaggy" Haywood" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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"?

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

Tell me if I'm wrong guys.


 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      07-18-2003
HongKongHooker <(E-Mail Removed)> scribbled the following:

> "Peter "Shaggy" Haywood" <(E-Mail Removed)> wrote in
> message news:(E-Mail Removed)...
>> 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?


> ++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 ((E-Mail Removed)) ---------------------------\
| 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
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      07-18-2003
"HongKongHooker" <(E-Mail Removed)> wrote:

> "Peter "Shaggy" Haywood" <(E-Mail Removed)> wrote in
> message news:(E-Mail Removed)...
> > 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?

>
> ++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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Effecient way of creating XML Pitch Java 27 10-06-2010 01:49 PM
Re: Effecient way of creating XML Arne Vajh°j Java 4 09-29-2010 08:56 PM
effecient template system DKode ASP .Net 2 06-08-2005 01:05 AM
Replacing palindrome substrings of an input string with a given string. Any effecient algorithm? Tung Chau C Programming 0 08-06-2004 10:18 AM
effecient software engineering Aaron Perl Misc 3 12-26-2003 03:45 PM



Advertisments