473,395 Members | 2,253 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,395 software developers and data experts.

Which is more effecient ?

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

Thanks.
Nov 13 '05 #1
10 3472
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

Nov 13 '05 #2
On 14 Jul 2003 13:49:00 -0700, in comp.lang.c ,
vi*******************@yahoo.com (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 =---
Nov 13 '05 #3
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.

Nov 13 '05 #4
In article <49*************************@posting.google.com> ,
vi*******************@yahoo.com (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.
Nov 13 '05 #5
Tom St Denis <to********@iahu.ca> 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
Nov 13 '05 #6
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"?
Nov 13 '05 #7

"Peter "Shaggy" Haywood" <ph******@alphalink.com.au.STOP.SPAM> wrote in
message news:3f**************@news.alphalink.com.au...
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.
Nov 13 '05 #8
HongKongHooker <su****@hooker.com> scribbled the following:
"Peter "Shaggy" Haywood" <ph******@alphalink.com.au.STOP.SPAM> wrote in
message news:3f**************@news.alphalink.com.au...
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 (pa*****@cc.helsinki.fi) ---------------------------\
| 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
Nov 13 '05 #9
"HongKongHooker" <su****@hooker.com> wrote:
"Peter "Shaggy" Haywood" <ph******@alphalink.com.au.STOP.SPAM> wrote in
message news:3f**************@news.alphalink.com.au...
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
Nov 13 '05 #10
HongKongHooker wrote:

re:
>Which is more efficient and why?
>p++ or ++p.
++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.]

--
Chris "Zero [Wendy Rule]" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
21
by: Rabbit63 | last post by:
Hi: I want to show a set of records in the database table on the clicnt browser. I have two ways to do this (writen in JScript): 1.The first way is: <% var sql = "select firstname from...
29
by: guru.slt | last post by:
"the c++ standard library, tutorial and reference" book, it says: ++i is faster than i++. the latter involves a temporary object because it must return the old value/object of i. for this reason,...
3
by: jw56578 | last post by:
Which way of retrieving a record is more effecient?: Select tbl1.field1, tbl2.field1 from table1 tbl1 inner join table2 tbl2 on tbl1.id = tbl2.id where someid = somevalue and someid =...
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
15
by: Rob Meade | last post by:
Hi all, I have a databse which I'm pulling the data from for my ASP page. I have 4 tables, Course, Feature, Objective, and PreRequisite. The last three all contain a course product code and a...
4
by: | last post by:
I am working with an application that requires multithreading. I have found a sample online that I am currently working with/learning from. However, I am not sure if this is the most effecient...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.