Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Which is faster?

Reply
Thread Tools

Which is faster?

 
 
lak
Guest
Posts: n/a
 
      07-29-2008
Which is faster? Post Increment or assignment? Why?
I was not able to get any things.
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      07-29-2008
lak wrote:

> 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

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      07-29-2008
lak wrote:

> 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?

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      07-29-2008
On 29 Jul, 13:31, lak <(E-Mail Removed)> wrote:

> 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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      07-29-2008
Nick Keighley wrote:
> lak <(E-Mail Removed)> wrote:
>
>> 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.


 
Reply With Quote
 
rahul
Guest
Posts: n/a
 
      07-30-2008
On Jul 29, 5:31*pm, lak <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      07-30-2008
rahul wrote:

> On Jul 29, 5:31*pm, lak <(E-Mail Removed)> wrote:
>> 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.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      07-30-2008
Dann Corbit wrote:
> 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.

 
Reply With Quote
 
Coos Haak
Guest
Posts: n/a
 
      07-30-2008
Op Tue, 29 Jul 2008 23:43:45 -0700 schreef Dann Corbit:

> "santosh" <(E-Mail Removed)> wrote in message
> news:g6ovf9$qbs$(E-Mail Removed)...
>> Dann Corbit wrote:
>>> 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.
>
> ** 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

 
Reply With Quote
 
moanga@gmail.com
Guest
Posts: n/a
 
      07-31-2008
On Jul 29, 9:21 pm, (E-Mail Removed) (Gordon Burditt) wrote:
> 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'.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Microcontrollers: which one ? which language ? which compiler ? The Jesus of Suburbia NZ Computing 2 02-11-2006 06:53 PM
Web Stats? Which to use? Which is best? Familyman HTML 3 02-09-2006 11:05 PM
ADSL WIC support - which NM's, and which IOS versions? Kralizec Craig Cisco 5 12-08-2005 02:20 AM
which XMI version compatible to which UML version? Kenny XML 0 06-02-2004 10:20 PM
Keeping track of which user controls need to be loaded and which not John ASP .Net 0 07-08-2003 09:26 AM



Advertisments