Is prefix faster than postfix?

This is a discussion on Is prefix faster than postfix? within the C Programming forums, part of the General Programming Boards category; Is Code: ++x; faster than Code: x++; ? I thought it might be, since ++x can do the increment right ...

  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046

    Is prefix faster than postfix?

    Is
    Code:
    ++x;
    faster than
    Code:
    x++;
    ? I thought it might be, since ++x can do the increment right then and there, while x++ has to do it later.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    gcc seems to use the same number of instructions for both. I tested with the following 2 programs:
    Code:
    itsme@itsme:~/C$ cat prefix.c
    int main(void) { int i = 0; int j; j = ++i; }
    itsme@itsme:~/C$ cat postfix.c
    int main(void) { int i = 0; int j; j = i++; }
    Then I compiled each with: gcc -S <filename>

    I won't spam the post by actually pasting the entire .s files, but only a few lines changed, and I'll post those:
    Code:
    // prefix.s
            leal    -4(%ebp), %eax
            incl    (%eax)
            movl    -4(%ebp), %eax
    Code:
    // postfix.s
            movl    -4(%ebp), %edx
            leal    -4(%ebp), %eax
            incl    (%eax)
    Those lines are in the same place as they are in the other file, so there's the same number of instuctions. The rest of the instructions are exactly the same.
    Last edited by itsme86; 07-27-2005 at 05:45 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Hmm, okay, thanks for going to so much trouble.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    You'll find that prefix is faster than postfix for some non-primitive datatypes in C++. (Yes I know this is the C forum.)

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by dwks
    Hmm, okay, thanks for going to so much trouble.


    Code:
    gcc -S some_prog.c

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    prefix is faster than postfix for some non-primitive datatypes in C++
    I know, that's why I was wondering if prefix was faster for ints too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,341
    The only real guarantee is that prefix will be no slower than postfix.

    In principle, x++ (for x an int) must be expanded to something like;
    Code:
       int temp = x;
       x = x+1;
       /* allow value of temp to be accessible */
    while ++x requires no temporary storage of the original value of x. A literal interpretation of that requirement would make postfix slower than prefix operations.

    However, compilers (or, more accurately, compiler writers) are smarter than that. Most mainstream compilers are able to recognise cases where the original value of x need not be accessible, and therefore avoid storing it's value temporarily. Which is the reason that the example given by itsme86 will usually exhibit no measurable difference in terms of number of instructions or performance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little question about postfix and prefix
    By firebird in forum C Programming
    Replies: 3
    Last Post: 05-08-2008, 07:27 AM
  2. Replies: 4
    Last Post: 03-12-2006, 01:17 PM
  3. postfix and prefix
    By drdodirty2002 in forum C++ Programming
    Replies: 8
    Last Post: 09-24-2004, 06:26 AM
  4. postfix and prefix...???
    By matheo917 in forum C++ Programming
    Replies: 8
    Last Post: 04-20-2002, 01:19 PM
  5. Data Structures / prefix and postfix
    By rickc77 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-08-2001, 11:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21