Which is faster?

C

Chris Dollin

lak said:
Which is faster? Post Increment or assignment? Why?
I was not able to get any things.

/Usually/ -- I very carefully am not saying /always/ -- the
timing difference between using an auto-increment/decrement,
pre or post, vs an assignment like `spoo += 1`, is not
detectable in a run of an application.

This is because compilers are pretty smart and (although they
may need a little encouragement) will compile the various
different ways of saying "increment spoo" into the same
code, if that's legal. And I/O isn't cheap.

And the Standard doesn't say /anything/ about what the relative
costs are.

So, if it matters -- I say, /if/ it matters -- the faster thing
to use, if there is one, has to be determined by experiment,
and you have to realise that which the faster one is can change
as soon as you change machines, compilers, compiler options,
memory size, air conditioning, or the colour of your tie.

Otherwise, which is to say, usually, pick whichever most
clearly expresses your intent to your audience.
 
S

santosh

lak said:
Which is faster? Post Increment or assignment?

You mean to ask which of

x = x + 1;

or

x++;

is faster? Not only are their semantics subtly different, their relative
costs are not mentioned in the C Standard. In fact the C Standard has
nothing at all to say about the "efficiency" of any function or
construct.

You should consult in a group for your implementation, but ultimately
the best method is to make your measurements. But be aware that no two
compilations of even an identical program are guaranteed to produce the
same machine code and the "speed" of the resulting executable will
further depend on things way beyond the scope of the C language, like
the operating system, hardware, cache effects etc.
Why? I was not able to get any things.

What "things" were you not able to "get"? Can you elaborate, and if
possible include code that illustrates your problem or question?
 
N

Nick Keighley

Which is faster? Post Increment or assignment? Why?
I was not able to get any things.

i=i+1 5 keystrokes
i+=1 4 keystrokes
i++ 3 keystrokes

Hence post-increment is faster than assignment
 
C

CBFalconer

Nick said:
i=i+1 5 keystrokes
i+=1 4 keystrokes
i++ 3 keystrokes

Hence post-increment is faster than assignment

Converting to complete statements, and better formatting:

i = i + 1; /* 10 keystrokes */
i += 1; /* 7 keystrokes */
i++; /* 4 keystrokes */
++i; /* 4 keystrokes */

with the same conclusion. Pre-increment is challenging.
 
R

rahul

Which is faster? Post Increment or assignment? Why?
I was not able to get any things.
By now you already have the answer that you can not say. You are
asking if
x = x + 1;
or
x++;
is faster. If the compiler choose to implement the first one as
mov temp, x -- copy x to temporary memory location
add temp, 1 -- add 1 to temp
mov x, temp -- copy temp to x
and the second one as
inc x -- increment x
then the second one may be faster on most architectures(may be -
because there are a lot of strings attached. Other posters have
already discussed them).

Generally you should not concern youself with these issues. Pre-mature
optimization is evil. Further, if you turn on the optimizations in
your compiler they are smart enough to generate identical code for
both of them.
 
S

santosh

rahul said:
By now you already have the answer that you can not say. You are
asking if
x = x + 1;
or
x++;
is faster. If the compiler choose to implement the first one as
mov temp, x -- copy x to temporary memory location
add temp, 1 -- add 1 to temp
mov x, temp -- copy temp to x
and the second one as
inc x -- increment x
then the second one may be faster on most architectures(may be -
because there are a lot of strings attached. Other posters have
already discussed them).

Generally you should not concern youself with these issues. Pre-mature
optimization is evil. Further, if you turn on the optimizations in
your compiler they are smart enough to generate identical code for
both of them.

I suspect that nearly all modern compilers would emit the same machine
instructions for x = x + 1, x += 1, x++, and ++x, even with all
optimisations turned off. This is basic stuff.
 
S

santosh

Dann said:
santosh wrote

[ ... ]
At least some of them will. Each increment in the C program below
results in the increment of a register in the assembly listing below.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int x = rand();
printf("x is %d\n", x);
x = x + 1;
printf("x is %d\n", x);
x += 1;
printf("x is %d\n", x);
x++;
printf("x is %d\n", x);
++x;
printf("x is %d\n", x);
return 0;
}

; Listing generated by Microsoft (R) Optimizing Compiler Version
15.00.21022.08
[ ... ]
; 8 : x = x + 1;
00018 46 inc esi

[ ... ]
; 10 : x += 1;
00021 46 inc esi

[ ... ]
; 12 : x++;
0002a 46 inc esi

[ ... ]
; 14 : ++x;
00033 46 inc esi

[ ... ]

I assume you did not enable optimisations? With optimisation for speed
turned on, I would've expected to see ADD instructions in the place of
the INCs.
 
C

Coos Haak

Op Tue, 29 Jul 2008 23:43:45 -0700 schreef Dann Corbit:
santosh said:
Dann said:
santosh wrote

[ ... ]
I suspect that nearly all modern compilers would emit the same
machine instructions for x = x + 1, x += 1, x++, and ++x, even with
all optimisations turned off. This is basic stuff.

At least some of them will. Each increment in the C program below
results in the increment of a register in the assembly listing below.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int x = rand();
printf("x is %d\n", x);
x = x + 1;
printf("x is %d\n", x);
x += 1;
printf("x is %d\n", x);
x++;
printf("x is %d\n", x);
++x;
printf("x is %d\n", x);
return 0;
}

; Listing generated by Microsoft (R) Optimizing Compiler Version
15.00.21022.08
[ ... ]
; 8 : x = x + 1;
00018 46 inc esi

[ ... ]
; 10 : x += 1;
00021 46 inc esi

[ ... ]
; 12 : x++;
0002a 46 inc esi

[ ... ]
; 14 : ++x;
00033 46 inc esi

[ ... ]

I assume you did not enable optimisations? With optimisation for speed
turned on, I would've expected to see ADD instructions in the place of
the INCs.

Optimization was on.
64 bit compiler mode, AMD64 instruction set.
I guess that INC is as fast as ADD.
At any rate, it's plenty fast most of the time.

<OT>
From the AMD documentation I learn that the single byte INC and DEC
instructions (40..4F) are used as REX prefixes. The double byte opcodes
form FF /0 and FF /1 are still available. I think the program would not run
in true 64 bit mode. I don't have a such a machine (yet) so I can't verify
this ;-)
</OT>
 
M

moanga

In the case of post increment vs. assignment, it does matter
whether the resulting value is used. The code for:
p++;
vs.
p += 1;
is hopefully identical.

That's horribly misleading. I'd expect the compiler to generate
identical code for 'p += 1' and '++p', NOT 'p++'. In fact, if the
resulting value is used, the generated code HAS to be different
for 'p++' and 'p += 1'.
 
B

Barry Schwarz

That's horribly misleading. I'd expect the compiler to generate
identical code for 'p += 1' and '++p', NOT 'p++'. In fact, if the
resulting value is used, the generated code HAS to be different
for 'p++' and 'p += 1'.

Since both statements are complete and the only effect of either is to
increment p, you have changed the intent of the question. If the code
in question had been
a = p++;
and
a = (p+=1);
your point would make sense.
 
K

Keith Thompson

See those semicolons? They are in there for a reason.
You can't use the value of p++; , an expression like foo(p++;)
won't compile.


Please explain how you can use the value with the semicolons left in.

I have restored the attribution lines that Gordon rudely deleted.

To answer your question, here's how:

x =
p++;

y =
p += 1;

:cool:}

(As always, permission to quote this article, or any other article I
post to Usenet, is granted *only* if all quoted text is properly
attributed.)
 
A

Ali Karaali

3.1.3.4 (C89): "An integer character constant is a sequence of one or more
multibyte characters enclosed in single-quotes, as in 'x' or 'ab'."

6.4.4.4(2) (C99): "An integer character constant is a sequence of one or
more multibyte characters enclosed in single-quotes, as in 'x'."

The difference between the above two quotes led me to poke around further
in C99:

I don!t understand what is the differance?
6.4.4.4(10): "The value of an integer character constant containing more
than one character (e.g., 'ab'), or containing a character or escape
sequence that does not map to a single-byte execution character, is
implementation-defined."

What does it mean?
 
W

Walter Roberson

Ali Karaali said:
alright than,
char ch = 'ab';
What does it mean?

Whatever the implementation defines it to mean. Could be any of
a number of things. Keep in mind that the char type could
potentially be as large as a long long, so 'ab' might be an error,
or might be the same as 'a' or might be the same as 'b', or
might be 256 * 'a' + 'b' or might be 256 * 'b' + 'a'
or perhaps the implementation might interpret a multi-byte constant
as loading the bytes in order from left to right, perhaps
resulting in 2^24 * 'a' + 2^16 * 'b' + 2^8 * 0 + 0,
or perhaps something else entirely....
 
W

Willem

Ali Karaali wrote:
) alright than,
)
) char ch = 'ab';
)
) What does it mean?

That's implementation-defined.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
K

Keith Thompson

Ali Karaali said:
alright than,

char ch = 'ab';

What does it mean?

It means you should decide what value you want to use to initialize
ch, and then find some other way to do it.

I've seen an almost-reasonable use of things like 'ab', where the only
real requirement was that different character constants would map
uniquely to distinct values. There's no guarantee of that, but it's
not too likely that a compiler would map two distinct constants to the
same value.

But still, a better way to accomplish the same thing would have been
to define a macro that takes 'a' and 'b' as arguments.
 
H

Harald van Dijk

It means it is up the implementation what it (say 'ab') actually means,
as is say '\q'.

'\q' is undefined, not implementation-defined, because it is not an escape
sequence at all according to the syntax. An escape sequence such as
'\u20ac' is what needs to be documented.
 
K

Keith Thompson

Ali Karaali said:
Is it same in C89?

Is what the same as what?

Please quote some context when you post a followup. The Google Groups
interface will do this for you automatically.

Looking back at the parent article, as far as I know, the rules for
character constants containing more than one character, such as 'ab',
did not change significantly between C90 and C99.
 

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

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top