Which is faster?

Discussion in 'C Programming' started by lak, Jul 29, 2008.

  1. lak

    lak Guest

    Which is faster? Post Increment or assignment? Why?
    I was not able to get any things.
     
    lak, Jul 29, 2008
    #1
    1. Advertising

  2. lak

    Chris Dollin Guest

    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
     
    Chris Dollin, Jul 29, 2008
    #2
    1. Advertising

  3. lak

    santosh Guest

    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?
     
    santosh, Jul 29, 2008
    #3
  4. On 29 Jul, 13:31, lak <> 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
     
    Nick Keighley, Jul 29, 2008
    #4
  5. lak

    CBFalconer Guest

    Nick Keighley wrote:
    > lak <> 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.
     
    CBFalconer, Jul 29, 2008
    #5
  6. lak

    rahul Guest

    On Jul 29, 5:31 pm, lak <> 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.
     
    rahul, Jul 30, 2008
    #6
  7. lak

    santosh Guest

    rahul wrote:

    > On Jul 29, 5:31 pm, lak <> 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.
     
    santosh, Jul 30, 2008
    #7
  8. lak

    santosh Guest

    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.
     
    santosh, Jul 30, 2008
    #8
  9. lak

    Coos Haak Guest

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

    > "santosh" <> wrote in message
    > news:g6ovf9$qbs$...
    >> 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
     
    Coos Haak, Jul 30, 2008
    #9
  10. lak

    Guest

    On Jul 29, 9:21 pm, (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'.
     
    , Jul 31, 2008
    #10
  11. On Thu, 31 Jul 2008 12:34:38 -0700 (PDT), wrote:

    >On Jul 29, 9:21 pm, (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'.


    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
     
    Barry Schwarz, Aug 1, 2008
    #11
  12. (Gordon Burditt) writes:
    > writes:
    >> On Jul 29, 9:21 pm, (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

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

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

    --
    Keith Thompson (The_Other_Keith) <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"
     
    Keith Thompson, Aug 1, 2008
    #12
  13. lak

    Ali Karaali Guest

    > 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?
     
    Ali Karaali, Aug 1, 2008
    #13
  14. lak

    Ali Karaali Guest

    alright than,

    char ch = 'ab';

    What does it mean?
     
    Ali Karaali, Aug 1, 2008
    #14
  15. In article <>,
    Ali Karaali <> wrote:

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

    --
    "There are some ideas so wrong that only a very intelligent person
    could believe in them." -- George Orwell
     
    Walter Roberson, Aug 1, 2008
    #15
  16. lak

    Willem Guest

    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
     
    Willem, Aug 1, 2008
    #16
  17. Ali Karaali <> writes:
    > 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) <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"
     
    Keith Thompson, Aug 1, 2008
    #17
  18. On Fri, 01 Aug 2008 17:13:41 -0400, Greg Comeau wrote:
    > In article
    > <>, Ali
    > Karaali <> wrote:
    >>> 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.
     
    Harald van Dijk, Aug 1, 2008
    #18
  19. lak

    Ali Karaali Guest

    Is it same in C89?
     
    Ali Karaali, Aug 1, 2008
    #19
  20. Ali Karaali <> writes:
    > 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) <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"
     
    Keith Thompson, Aug 2, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weng Tianxiang
    Replies:
    12
    Views:
    1,689
  2. Andreas Klemt
    Replies:
    1
    Views:
    463
    Steve C. Orr, MCSD
    Jul 23, 2003
  3. S. Justin Gengo
    Replies:
    2
    Views:
    376
    S. Justin Gengo
    Aug 20, 2003
  4. Bob
    Replies:
    1
    Views:
    2,720
  5. luca

    which one is faster?

    luca, Mar 7, 2004, in forum: Java
    Replies:
    1
    Views:
    342
    William Brogden
    Mar 8, 2004
Loading...

Share This Page