L
lak
Which is faster? Post Increment or assignment? Why?
I was not able to get any things.
I was not able to get any things.
lak said:Which is faster? Post Increment or assignment? Why?
I was not able to get any things.
lak said:Which is faster? Post Increment or assignment?
Why? I was not able to get any things.
Which is faster? Post Increment or assignment? Why?
I was not able to get any things.
Nick said:i=i+1 5 keystrokes
i+=1 4 keystrokes
i++ 3 keystrokes
Hence post-increment is faster than assignment
By now you already have the answer that you can not say. You areWhich is faster? Post Increment or assignment? Why?
I was not able to get any things.
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.
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
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.
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'.
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.
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:
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."
Ali Karaali said:alright than,
char ch = 'ab';
What does it mean?
Ali Karaali said:alright than,
char ch = 'ab';
What does it mean?
It means it is up the implementation what it (say 'ab') actually means,
as is say '\q'.
Ali Karaali said:Is it same in C89?
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.