473,498 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

++x is more optimised than x++ ???

I came across a nice example and info that claim that ++x is optimised
than x++.
Is it so ?

For example.
for(x = 0; x < 10; x++)
There is a simple fix for this, and it will optimize your loop, ever
so slightly.
for(x = 0; x < 10; ++x)
It can save milliseconds on your code rotations, for those using for
loops like this, this is a much faster way to parse through your
data.

Is it so ? How is it possible that ++x is faster than x++ ?

Thx in advans,
Karthik Balaguru

Sep 4 '07 #1
15 2782
karthikbalaguru wrote:
I came across a nice example and info that claim that ++x is optimised
than x++.
Is it so ?

For example.
for(x = 0; x < 10; x++)
There is a simple fix for this, and it will optimize your loop, ever
so slightly.
for(x = 0; x < 10; ++x)
It can save milliseconds on your code rotations, for those using for
loops like this, this is a much faster way to parse through your
data.

Is it so ? How is it possible that ++x is faster than x++ ?

Thx in advans,
Karthik Balaguru
In theory, ++c just increments c. c++ increments c and returns
the OLD value, so the old value must be saved.

In practice there isn't any difference for most compilers. If
the old value is discarded, they throw the saving of the
old value away and generate the same code as for ++c.
Sep 4 '07 #2
jacob navia wrote:
karthikbalaguru wrote:
>I came across a nice example and info that claim that ++x is optimised
than x++.
Is it so ?

For example.
for(x = 0; x < 10; x++)
There is a simple fix for this, and it will optimize your loop, ever
so slightly.
for(x = 0; x < 10; ++x)
It can save milliseconds on your code rotations, for those using for
loops like this, this is a much faster way to parse through your
data.

Is it so ? How is it possible that ++x is faster than x++ ?

Thx in advans,
Karthik Balaguru

In theory, ++c just increments c. c++ increments c and returns
the OLD value, so the old value must be saved.

In practice there isn't any difference for most compilers. If
the old value is discarded, they throw the saving of the
old value away and generate the same code as for ++c.
It would require a different example, to show an advantage for ++x on
CPUs which support it better in the instruction set, like old PPC Macs.
Sep 4 '07 #3
"karthikbalaguru" <ka***************@gmail.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
I came across a nice example and info that claim that ++x is
optimised than x++. Is it so ?

For example.
for(x = 0; x < 10; x++)
There is a simple fix for this, and it will optimize your loop, ever
so slightly.
for(x = 0; x < 10; ++x)
It can save milliseconds on your code rotations, for those using for
loops like this, this is a much faster way to parse through your
data.

Is it so ? How is it possible that ++x is faster than x++ ?
Decades ago, when compilers were pretty bad at optimizing, that was likely
true. Nowadays, they should compile to identical instructions except in
cases where the resulting value of the expression matters. For instance, "y
= ++x;" is likely to still be faster than "y = x++;", but the two statements
don't do the same thing.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from http://www.teranews.com

Sep 4 '07 #4
On Tue, 04 Sep 2007 17:42:04 +0000, karthikbalaguru wrote:
I came across a nice example and info that claim that ++x is optimised
than x++.
Is it so ?

For example.
for(x = 0; x < 10; x++)
There is a simple fix for this, and it will optimize your loop, ever
so slightly.
for(x = 0; x < 10; ++x)
It can save milliseconds on your code rotations, for those using for
loops like this, this is a much faster way to parse through your
data.

Is it so ? How is it possible that ++x is faster than x++ ?
The reasoning probably goes something like this:

var = ++x stores the incremented value of x in var
var = x++ stores the unincremented value of x in var
some compilers may generate a temporary for this:
temp = x
x++
var = temp
thus you should use ++x instead of x++

The main problem with this is that any compiler worth its salt won't
bother unless the unincremented value is actually needed; in the loop,
it's not. The value of x++ isn't used at all, no need to store the value,
so why bother with the overhead?

A quick test with gcc shows it generating identical code in both cases;
I'd expect the same from pretty much any compiler these days.
Sep 4 '07 #5

On Sep 4, 7:42 pm, karthikbalaguru <karthikbalagur...@gmail.com>
wrote:
I came across a nice example and info that claim that ++x is optimised
than x++.
Is it so ?

For example.
for(x = 0; x < 10; x++)
There is a simple fix for this, and it will optimize your loop, ever
so slightly.
for(x = 0; x < 10; ++x)
It can save milliseconds on your code rotations, for those using for
loops like this, this is a much faster way to parse through your
data.

Is it so ? How is it possible that ++x is faster than x++ ?

Thx in advans,
Karthik Balaguru
Perhaps your post would have more sense in lang.c++, as there there IS
a diference, regardless of the compiler, due to operator overloadings.

Mariano

Sep 5 '07 #6
>I came across a nice example and info that claim that ++x is optimised
>than x++.
If the value of the expression is not used, the generated code for
++x may be more the same as the code for x++ than the generated
code for x++ is the same as the code for ++x. (Note: Standard C
does not define the "is more equal" operator.) Reasonable compilers
will generate identical code in this case. Identical code is *NOT*
guaranteed to execute at identical speed (consider pipelining, and
cache contents of previous code, task switches, interrupts, etc).
>Is it so ?
Any statement of the form "A is faster than B, A is slower than B,
or A is about the same speed as B" is false (even if B is "Repeat
A one million times"), unless the conditions of test such as CPU,
compiler version AND checksum, and compile-time options are specified.
>For example.
for(x = 0; x < 10; x++)
There is a simple fix for this, and it will optimize your loop, ever
so slightly.
There is no guarantee of that. Reasonable compilers will generate
identical code in both cases.
>for(x = 0; x < 10; ++x)
It can save milliseconds on your code rotations, for those using for
loops like this, this is a much faster way to parse through your
data.
Or it could COST kiloseconds.
>Is it so ? How is it possible that ++x is faster than x++ ?
Standard C does not guarantee anything about performance.

Sep 5 '07 #7
On Tue, 04 Sep 2007 17:42:04 -0000, karthikbalaguru
<ka***************@gmail.comwrote in comp.lang.c:
I came across a nice example and info that claim that ++x is optimised
than x++.
Whose nice example? Why do you think it is reliable? The net is full
of junk. When it comes to programming, more bad than good.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Sep 5 '07 #8
Tim Prince <timothypri...@sbcglobal.netwrote:
jacob navia wrote:
karthikbalaguru wrote:
I came across a nice example and info that claim that
++x is optimised than x++.
Is it so ?
Note that the answer in C++ is different because x may be
a class with overloaded increment operators.
For example.
for(x = 0; x < 10; x++)
There is a simple fix for this,
It's not a _fix_, it's a micro-optimisation.
and it will optimize your loop, ever so slightly.
It may make it faster, it may make it slower. But if the
change will only save 20 seconds cpu time over the 10 years
the program may typically be employed, then it's not even
worth spending 2 minutes profiling!
for(x = 0; x < 10; ++x)
It can save milliseconds on your code rotations, for
those using for loops like this, this is a much
faster way to parse through your data.
>
Is it so ? How is it possible that ++x is faster than x++?
In theory, ++c just increments c. c++ increments c and returns
the OLD value, so the old value must be saved.

In practice there isn't any difference for most compilers. If
the old value is discarded, they throw the saving of the
old value away and generate the same code as for ++c.

It would require a different example, to show an advantage for
++x on CPUs which support it better in the instruction set,
like old PPC Macs.
The difference is usually much clearer with pointer increments...

while (*s++ = *t++) /* mac 68k */
;

verses

if (*s = *t) /* pentium & ppc */
while (*++s = *++t)
;

You will find cases where compiler optimisation isn't perfect
at things like...

while (i-- < 10)
...stuff...

....but it's usually a compiler issue, not an instruction set
issue.

--
Peter

Sep 5 '07 #9
Peter Nilsson wrote:
The difference is usually much clearer with pointer increments...

while (*s++ = *t++) /* mac 68k */
;

verses

if (*s = *t) /* pentium & ppc */
while (*++s = *++t)
;

You will find cases where compiler optimisation isn't perfect
at things like...

while (i-- < 10)
...stuff...

...but it's usually a compiler issue, not an instruction set
issue.

--
Peter
It often /is/ an instruction set issue.
If memory serves me right, e.g. in ARM in Thumb mode pointers are
optimized for post-increment and pre-decrement.
It's a different story that a good compiler would virtually rewrite your
snippet of code in an equivalent and faster-for-this-machine way.
To do so, it needs to recognize your snippet as transformable: it's the
best reason to write the code in cliches (common idioms) and avoid
clever constructs and blocks nested CHAR_BIT deep :)
-- Ark
Sep 5 '07 #10
Kathik:
Is it so ? How is it possible that ++x is faster than x++ ?

As regards C++, either could be more efficient depending upon the
user's definition of the overloaded operator function. And, in
general, the user's prefix version tends to be faster than the postfix
version because the postfix version tends to involve creating a copy.

But we're talking about C here.

The purpose of x++ is to take the value, then increment it. (Quite
easily understood when reading from left to right).

The purpose of ++x is to increment it, then take the value. (Quite
easily understood when reading from left to right).

Now ask yourself, why should opening a door and then closing a window
be any faster than closing a window and then opening a door? (No smart-
allec answers thank you very much, such as that it depends whether
you're downstairs and the window is upstairs).

In the following code:

Func(x++);

, why should there be any kind of copy? Why wouldn't it simply be
treated as "take the value, then increment"? i.e.:

Func(x); ++x;

Anyway, to answer your question, if any compiler even makes a
difference between the two expressions (assuming the value of the
expression is discarded), then I'd probably get myself a different
compiler.

Martin
Sep 6 '07 #11
Martin Wells wrote:
>
Kathik:
Is it so ? How is it possible that ++x is faster than x++ ?

As regards C++, either could be more efficient depending upon the
user's definition of the overloaded operator function. And, in
general, the user's prefix version tends to be faster than the postfix
version because the postfix version tends to involve creating a copy.

But we're talking about C here.

The purpose of x++ is to take the value, then increment it. (Quite
easily understood when reading from left to right).

The purpose of ++x is to increment it, then take the value. (Quite
easily understood when reading from left to right).

Now ask yourself, why should opening a door and then closing a window
be any faster than closing a window and then opening a door?
(No smart-allec answers thank you very much,
such as that it depends whether
you're downstairs and the window is upstairs).

In the following code:

Func(x++);

, why should there be any kind of copy? Why wouldn't it simply be
treated as "take the value, then increment"? i.e.:

Func(x); ++x;
That doesn't mean the same thing.
There's a sequence point between argument evaluation
and the actual function call.

(++x, Func(x - 1)) means the same thing as Func(x++).

/* BEGIN new.c output */

10
00
10

/* END new.c output */
/* BEGIN new.c */

#include <stdio.h>

void Func(int y);

int x;

int main(void)
{
puts("/* BEGIN new.c output */\n");

x = 0;
Func(x++);

x = 0;
Func(x); ++x;

x = 0;
(++x, Func(x - 1));

puts("\n/* END new.c output */");
return 0;
}

void Func(int y)
{
printf("%d%d\n", x, y);
}

/* END new.c */
--
pete
Sep 6 '07 #12
pete:
Func(x); ++x;

That doesn't mean the same thing.
There's a sequence point between argument evaluation
and the actual function call.

I over-simplified the example for sake of the discussion.

Martin

Sep 6 '07 #13
Martin Wells <wa****@eircom.netwrites:
pete:
Func(x); ++x;

That doesn't mean the same thing.
There's a sequence point between argument evaluation
and the actual function call.


I over-simplified the example for sake of the discussion.

Martin
This is not, as you have discovered, the place to do that. Although in
this case I think it was worth highlighting the difference as it would
be very instructive for a newbie learning C and convincingly
demonstrates sequencing in a simple, straightforward manner.

Sep 6 '07 #14
On Wed, 05 Sep 2007 17:13:25 -0700, Martin Wells wrote:
In the following code:

Func(x++);

, why should there be any kind of copy? Why wouldn't it simply be
treated as "take the value, then increment"? i.e.:

Func(x); ++x;
What if Func somehow knows x's address? There is a sequence point
after the arguments are evaluated and before the function is
called, so it should see the incremented value.

Maybe ++x, Func(x - 1); would do it.
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 6 '07 #15
Richard wrote:
>
Martin Wells <wa****@eircom.netwrites:
pete:
Func(x); ++x;

That doesn't mean the same thing.
There's a sequence point between argument evaluation
and the actual function call.

I over-simplified the example for sake of the discussion.

Martin

This is not, as you have discovered, the place to do that. Although in
this case I think it was worth highlighting the difference as it would
be very instructive for a newbie learning C and convincingly
demonstrates sequencing in a simple, straightforward manner.
And I want to restate that
(++x, Func(x - 1)) means the same thing as Func(x++)
and also say:
that the two expressions mean the same thing
regardless of the return type of Func and
regardless of whether or not x is an expression of type int
with an initial value of INT_MAX.

--
pete
Sep 7 '07 #16

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

Similar topics

303
17412
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
11
1797
by: junky_fellow | last post by:
What are the basic guidelines to write an optimised code. What points should one keep in mind for this ? Is this purely architecture or complier specific ? Are there any general techniques that...
5
2836
by: John Smith | last post by:
Sorry if this is the wrong forum... Does anyone know what the difference is between a debug build and an optimised debug build in Visual Studio 2003?
1
1034
by: mark | r | last post by:
anyone know a tutorial or sample code for uploading an image to a web server and optimising it and saving the optimised file to a folder? also can you mix .net and regular asp on the same page? ...
2
2460
by: hsimon | last post by:
Hi, I have no DB2 or SAP knowledge, but would like to know if someone has done Quickshadow Space optimised snapshot of SAP/DB2 and did a restore from the snapshots. We try to backup SAP/DB2, by...
4
2151
by: Michael | last post by:
Hi! (OK, slightly silly subject line :) I'm extremely pleased to say - Kamaelia 0.4.0 has been released! What's New & Changed? =====================
9
2629
by: master | last post by:
Actually, it is not only the record locking, what I need, and nobody seems to descibe this. Imagine the following scenario. There is a database with, say 10000 records with some unvalidated...
22
1955
by: karthikbg | last post by:
Hi, I have a Board of Coldfire communicating with a PC on the other side. The PC will be sending via TCP/IP some 5000 different command identification numbers only ( consider 1 to 5000 ) at 1...
0
7125
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
7002
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
7165
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
7205
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...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.