Connecting Tech Pros Worldwide Forums | Help | Site Map

Which is faster?

lak
Guest
 
Posts: n/a
#1: Jul 29 '08
Which is faster? Post Increment or assignment? Why?
I was not able to get any things.

Chris Dollin
Guest
 
Posts: n/a
#2: Jul 29 '08

re: Which is faster?


lak wrote:
Quote:
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.

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

santosh
Guest
 
Posts: n/a
#3: Jul 29 '08

re: Which is faster?


lak wrote:
Quote:
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.
Quote:
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?

Nick Keighley
Guest
 
Posts: n/a
#4: Jul 29 '08

re: Which is faster?


On 29 Jul, 13:31, lak <lakindi...@gmail.comwrote:
Quote:
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

--
Nick Keighley

Gordon Burditt
Guest
 
Posts: n/a
#5: Jul 29 '08

re: Which is faster?


>Which is faster? Post Increment or assignment? Why?

Given two tasks, A and B, the C standard does not guarantee anything
about the relative speed of the two, even if B is "repeat A one
billion times". The result is also likely to depend on the instruction
set of the target machine, and the optimization settings of this
invocation of the compiler.

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. However, identical code does not guarantee
identical performance, as the prior state of the cache and execution
state of the processor may change that time.
Quote:
>I was not able to get any things.
Those things belong to someone else, and scams with Paypal do
not entitle you to take them.

Greg Comeau
Guest
 
Posts: n/a
#6: Jul 29 '08

re: Which is faster?


In article <85bf0c41-c23f-42b7-bc8f-b333ce11a3a7@b30g2000prf.googlegroups.com>,
lak <lakindia89@gmail.comwrote:
Quote:
>Which is faster? Post Increment or assignment? Why?
>I was not able to get any things.
If I understand your questions (and I may not) generally
speaking you don't know. And the Standard does not dictate
such. That said, most compilers will detect any equivalences
possible and generate the exact same code on situation such
as this. As usual, even if that is the case, there are always
exceptions to rules so tread cautiously if you're assuming
this should matter.
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
CBFalconer
Guest
 
Posts: n/a
#7: Jul 29 '08

re: Which is faster?


Nick Keighley wrote:
Quote:
lak <lakindi...@gmail.comwrote:
>
Quote:
>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
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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


rahul
Guest
 
Posts: n/a
#8: Jul 30 '08

re: Which is faster?


On Jul 29, 5:31*pm, lak <lakindi...@gmail.comwrote:
Quote:
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.

santosh
Guest
 
Posts: n/a
#9: Jul 30 '08

re: Which is faster?


rahul wrote:
Quote:
On Jul 29, 5:31*pm, lak <lakindi...@gmail.comwrote:
Quote:
>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.
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.

Dann Corbit
Guest
 
Posts: n/a
#10: Jul 30 '08

re: Which is faster?


"santosh" <santosh.k83@gmail.comwrote in message
news:g6ot3d$6ma$1@registered.motzarella.org...
Quote:
rahul wrote:
>
Quote:
>On Jul 29, 5:31 pm, lak <lakindi...@gmail.comwrote:
Quote:
>>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.
>
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

TITLE c:\tmp\t.c
.686P
.XMM
include listing.inc
.model flat

INCLUDELIB OLDNAMES

EXTRN @__security_check_cookie@4:PROC
EXTRN __imp__printf:PROC
EXTRN __imp__rand:PROC
$SG-5 DB 'x is %d', 0aH, 00H
ORG $+3
$SG-6 DB 'x is %d', 0aH, 00H
ORG $+3
$SG-7 DB 'x is %d', 0aH, 00H
ORG $+3
$SG-8 DB 'x is %d', 0aH, 00H
ORG $+3
$SG-9 DB 'x is %d', 0aH, 00H
PUBLIC _main
; Function compile flags: /Ogtpy
; File c:\tmp\t.c
; COMDAT _main
_TEXT SEGMENT
_main PROC ; COMDAT

; 5 : {

00000 56 push esi
00001 57 push edi

; 6 : int x = rand();

00002 ff 15 00 00 00
00 call DWORD PTR __imp__rand

; 7 : printf("x is %d\n", x);

00008 8b 3d 00 00 00
00 mov edi, DWORD PTR __imp__printf
0000e 8b f0 mov esi, eax
00010 56 push esi
00011 68 00 00 00 00 push OFFSET $SG-5
00016 ff d7 call edi

; 8 : x = x + 1;

00018 46 inc esi

; 9 : printf("x is %d\n", x);

00019 56 push esi
0001a 68 00 00 00 00 push OFFSET $SG-6
0001f ff d7 call edi

; 10 : x += 1;

00021 46 inc esi

; 11 : printf("x is %d\n", x);

00022 56 push esi
00023 68 00 00 00 00 push OFFSET $SG-7
00028 ff d7 call edi

; 12 : x++;

0002a 46 inc esi

; 13 : printf("x is %d\n", x);

0002b 56 push esi
0002c 68 00 00 00 00 push OFFSET $SG-8
00031 ff d7 call edi

; 14 : ++x;

00033 46 inc esi

; 15 : printf("x is %d\n", x);

00034 56 push esi
00035 68 00 00 00 00 push OFFSET $SG-9
0003a ff d7 call edi
0003c 83 c4 28 add esp, 40 ; 00000028H
0003f 5f pop edi

; 16 : return 0;

00040 33 c0 xor eax, eax
00042 5e pop esi

; 17 : }

00043 c3 ret 0
_main ENDP
_TEXT ENDS
END

** Posted from http://www.teranews.com **
santosh
Guest
 
Posts: n/a
#11: Jul 30 '08

re: Which is faster?


Dann Corbit wrote:
Quote:
santosh wrote
[ ... ]
Quote:
Quote:
>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
[ ... ]
Quote:
; 8 : x = x + 1;
00018 46 inc esi
[ ... ]
Quote:
; 10 : x += 1;
00021 46 inc esi
[ ... ]
Quote:
; 12 : x++;
0002a 46 inc esi
[ ... ]
Quote:
; 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.

Dann Corbit
Guest
 
Posts: n/a
#12: Jul 30 '08

re: Which is faster?


"santosh" <santosh.k83@gmail.comwrote in message
news:g6ovf9$qbs$1@registered.motzarella.org...
Quote:
Dann Corbit wrote:
Quote:
>santosh wrote
>
[ ... ]
>
Quote:
Quote:
>>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
[ ... ]
>
Quote:
>; 8 : x = x + 1;
> 00018 46 inc esi
>
[ ... ]
>
Quote:
>; 10 : x += 1;
> 00021 46 inc esi
>
[ ... ]
>
Quote:
>; 12 : x++;
> 0002a 46 inc esi
>
[ ... ]
>
Quote:
>; 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.

** Posted from http://www.teranews.com **
Coos Haak
Guest
 
Posts: n/a
#13: Jul 30 '08

re: Which is faster?


Op Tue, 29 Jul 2008 23:43:45 -0700 schreef Dann Corbit:
Quote:
"santosh" <santosh.k83@gmail.comwrote in message
news:g6ovf9$qbs$1@registered.motzarella.org...
Quote:
>Dann Corbit wrote:
Quote:
>>santosh wrote
>>
>[ ... ]
>>
Quote:
>>>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
>[ ... ]
>>
Quote:
>>; 8 : x = x + 1;
>> 00018 46 inc esi
>>
>[ ... ]
>>
Quote:
>>; 10 : x += 1;
>> 00021 46 inc esi
>>
>[ ... ]
>>
Quote:
>>; 12 : x++;
>> 0002a 46 inc esi
>>
>[ ... ]
>>
Quote:
>>; 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.
>
** Posted from http://www.teranews.com **
<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>

--
Coos

rio
Guest
 
Posts: n/a
#14: Jul 30 '08

re: Which is faster?



"Nick Keighley" <nick_keighley_nospam@hotmail.comha scritto nel messaggio
news:f400be6d-28c7-4262-9600-e23773067a0f@l42g2000hsc.googlegroups.com...
Quote:
On 29 Jul, 13:31, lak <lakindi...@gmail.comwrote:
>
Quote:
>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
yes
Quote:
--
Nick Keighley
>


moanga@gmail.com
Guest
 
Posts: n/a
#15: Jul 31 '08

re: Which is faster?


On Jul 29, 9:21 pm, gordonb.4z...@burditt.org (Gordon Burditt) wrote:
Quote:
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'.
Greg Comeau
Guest
 
Posts: n/a
#16: Jul 31 '08

re: Which is faster?


In article <3038b1a0-ac91-4fc1-af37-f9bfca84ee8a@a1g2000hsb.googlegroups.com>,
<moanga@gmail.comwrote:
Quote:
>On Jul 29, 9:21 pm, gordonb.4z...@burditt.org (Gordon Burditt) wrote:
Quote:
>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'.
If not used, one would expect the same code. Once it's used,
you're right, it stops becoming identical. Which means you're
right, the example can be misleading because it can depend upon
how the expressions are used, or not.
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Malcolm McLean
Guest
 
Posts: n/a
#17: Jul 31 '08

re: Which is faster?



"lak" <lakindia89@gmail.comwrote in message news:
Quote:
Which is faster? Post Increment or assignment? Why?
I was not able to get any things.
>
In C both operations take the same time.
(Pedants, fire)

You might be misled because in C++ overloaded preincrement tends to be more
efficient. That's because in an overloaded expression like

if(obj++ < anotherobject)

the overloaded obj++ requires a temporary copy to be made for the
comparison.
In

if(++obj < anotherobject)

no copy is required. The ++ executes first and ots result is passed to the
overloaded comparison fucntion.


--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Greg Comeau
Guest
 
Posts: n/a
#18: Jul 31 '08

re: Which is faster?


In article <tqSdnRjfl7kevA_VnZ2dnUVZ8qPinZ2d@bt.com>,
Malcolm McLean <regniztar@btinternet.comwrote:
Quote:
>
>"lak" <lakindia89@gmail.comwrote in message news:
Quote:
>Which is faster? Post Increment or assignment? Why?
>I was not able to get any things.
>>
>In C both operations take the same time.
>(Pedants, fire)
>
>You might be misled because in C++ overloaded preincrement tends to be more
>efficient. That's because in an overloaded expression like
>
>if(obj++ < anotherobject)
>
>the overloaded obj++ requires a temporary copy to be made for the
>comparison.
>In
>
>if(++obj < anotherobject)
>
>no copy is required. The ++ executes first and ots result is passed to the
>overloaded comparison fucntion.
Which moves way on from the original focused question which
as if I recall only dealt with a simply type and a very
focused operation of it, and futhermore was a C only question.
That said, there is still room for the equivalent when it
can be seem that the ultimate thing being yo-yo about is
say only a simple type, though since this is a C forum,
let's no go there....
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Gordon Burditt
Guest
 
Posts: n/a
#19: Jul 31 '08

re: Which is faster?


>In the case of post increment vs. assignment, it does matter
Quote:
Quote:
>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
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.
Quote:
>resulting value is used, the generated code HAS to be different
>for 'p++' and 'p += 1'.
Please explain how you can use the value with the semicolons left in.

Barry Schwarz
Guest
 
Posts: n/a
#20: Aug 1 '08

re: Which is faster?


On Thu, 31 Jul 2008 12:34:38 -0700 (PDT), moanga@gmail.com wrote:
Quote:
>On Jul 29, 9:21 pm, gordonb.4z...@burditt.org (Gordon Burditt) wrote:
Quote:
>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'.
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.

--
Remove del for email
CBFalconer
Guest
 
Posts: n/a
#21: Aug 1 '08

re: Which is faster?


Malcolm McLean wrote:
Quote:
"lak" <lakindia89@gmail.comwrote:
>
Quote:
>Which is faster? Post Increment or assignment? Why?
>I was not able to get any things.
>
.... snip ...
Quote:
>
if(obj++ < anotherobject)
>
the overloaded obj++ requires a temporary copy to be made for the
comparison. In
>
if(++obj < anotherobject)
>
no copy is required. The ++ executes first and ots result is passed
to the overloaded comparison fucntion.
Not necessarily. That depends on the registers available, and the
operations possible between registers and memory. For example:

(++obj < memobj)

can compile to:

r0 = memobj + 1;
memobj = r0; leaving obj+1 in r0 for use

but

(obj++ < memobj)

can compile to:

r0 = memobj;
memobj = r0 + 1; leaving obj in r0 for use.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **
pete
Guest
 
Posts: n/a
#22: Aug 1 '08

re: Which is faster?


moanga@gmail.com wrote:
Quote:
On Jul 29, 9:21 pm, gordonb.4z...@burditt.org (Gordon Burditt) wrote:
Quote:
>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'.
There is no "if the resulting value is used".

The code examples given above, were statements.

p += 1; /* a statement */
(p += 1) /* an expression */
'p += 1' /* not a c construct */

--
pete
Richard Heathfield
Guest
 
Posts: n/a
#23: Aug 1 '08

re: Which is faster?


pete said:

<snip>
Quote:
p += 1; /* a statement */
Right.
Quote:
(p += 1) /* an expression */
Right.
Quote:
'p += 1' /* not a c construct */
Er, wrong.

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:

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

Thus, 'p += 1' /is/ a C construct, albeit not a terribly useful one.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Keith Thompson
Guest
 
Posts: n/a
#24: Aug 1 '08

re: Which is faster?


gordonb.fq0q5@burditt.org (Gordon Burditt) writes:
Quote:
moanga@gmail.com writes:
Quote:
>On Jul 29, 9:21 pm, gordonb.4z...@burditt.org (Gordon Burditt) wrote:
Quote:
>>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
>
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.
>
Quote:
>>resulting value is used, the generated code HAS to be different
>>for 'p++' and 'p += 1'.
>
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;

8-)}

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

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Ali Karaali
Guest
 
Posts: n/a
#25: Aug 1 '08

re: Which is faster?


3.1.3.4 (C89): "An integer character constant is a sequence of one or more
Quote:
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?
Quote:
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?
Greg Comeau
Guest
 
Posts: n/a
#26: Aug 1 '08

re: Which is faster?


In article <d0e7b0f3-0a2d-4cd1-9a3e-5dd055b29488@26g2000hsk.googlegroups.com>,
Ali Karaali <alicpp@gmail.comwrote:
Quote:
Quote:
>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?
That apparently they hoisted out the discussion about 'ab' to
a paragraph by itself, which you see below:
Quote:
Quote:
>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?
It means it is up the implementation what it (say 'ab') actually means,
as is say '\q'.
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Ali Karaali
Guest
 
Posts: n/a
#27: Aug 1 '08

re: Which is faster?


alright than,

char ch = 'ab';

What does it mean?
Richard Heathfield
Guest
 
Posts: n/a
#28: Aug 1 '08

re: Which is faster?


Ali Karaali said:
Quote:
alright than,
>
char ch = 'ab';
>
What does it mean?
As Greg already explained, what 'ab' actually means is up to the
implementation. Consult your documentation.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Willem
Guest
 
Posts: n/a
#29: Aug 1 '08

re: Which is faster?


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
Keith Thompson
Guest
 
Posts: n/a
#30: Aug 1 '08

re: Which is faster?


Ali Karaali <alicpp@gmail.comwrites:
Quote:
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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Harald van =?UTF-8?b?RMSzaw==?=
Guest
 
Posts: n/a
#31: Aug 1 '08

re: Which is faster?


On Fri, 01 Aug 2008 17:13:41 -0400, Greg Comeau wrote:
Quote:
In article
<d0e7b0f3-0a2d-4cd1-9a3e-5dd055b29488@26g2000hsk.googlegroups.com>, Ali
Karaali <alicpp@gmail.comwrote:
Quote:
Quote:
>>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?
>
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.
Ali Karaali
Guest
 
Posts: n/a
#32: Aug 1 '08

re: Which is faster?


Is it same in C89?
CBFalconer
Guest
 
Posts: n/a
#33: Aug 2 '08

re: Which is faster?


Ali Karaali wrote:
Quote:
>
Is it same in C89?
Is what the same as what? You have neglected to quote properly.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


** Posted from http://www.teranews.com **
Keith Thompson
Guest
 
Posts: n/a
#34: Aug 2 '08

re: Which is faster?


Ali Karaali <alicpp@gmail.comwrites:
Quote:
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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
rio
Guest
 
Posts: n/a
#35: Aug 2 '08

re: Which is faster?


"Gordon Burditt" <gordonb.fq0q5@burditt.orgha scritto nel messaggio
news:wrydnTASga6GuA_VnZ2dnUVZ_ovinZ2d@posted.inter netamerica...
Quote:
Quote:
Quote:
>>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
>
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.
>
Quote:
>>resulting value is used, the generated code HAS to be different
>>for 'p++' and 'p += 1'.
>
Please explain how you can use the value with the semicolons left in.
i think it is possible to do without semicolon

p-- p+=9 f( 76, 909 , others , 98* 87 +88 )
the end of instruction char is " " or "\n"
it not is betwen spaired () or []
not after instructions if(), while(), for()
last parentesis
not at end of definition int, char etc
while(a<b) --b
k[a*b + c]=d
while( a < b ){ --b f(b) }
for(a=3 a<9 ++a) f(a)
n=(a, b) d=h





Greg Comeau
Guest
 
Posts: n/a
#36: Aug 2 '08

re: Which is faster?


In article <91543$48938c6c$541dfcd3$21308@cache1.tilbu1.nb.ho me.nl>,
Harald van =?UTF-8?b?RMSzaw==?= <truedfx@gmail.comwrote:
Quote:
>On Fri, 01 Aug 2008 17:13:41 -0400, Greg Comeau wrote:
Quote:
>In article
><d0e7b0f3-0a2d-4cd1-9a3e-5dd055b29488@26g2000hsk.googlegroups.com>, Ali
>Karaali <alicpp@gmail.comwrote:
Quote:
>>>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?
>>
>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.
I could be wrong, but I believe there's two issues on the table.
One is what happens with \q, the other is what the value of the
thing that used the \q is. I seem to recall that \q requires
a diagnostic, but I could be recalling that incorrectly, because
I also seem to recall it being an allowable extension.
Maybe I'm just tired... :)
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Greg Comeau
Guest
 
Posts: n/a
#37: Aug 2 '08

re: Which is faster?


In article <lntze4e1ja.fsf@nuthaus.mib.org>,
Keith Thompson <kst-u@mib.orgwrote:
Quote:
>Ali Karaali <alicpp@gmail.comwrites:
Quote:
>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.
Richard posted the difference... in this thread.
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Closed Thread