postfix vs prefix operators

This is a discussion on postfix vs prefix operators within the C++ Programming forums, part of the General Programming Boards category; Greetings all. I have what may seem to be a simple question on C++ that I would really like to ...

  1. #1
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875

    postfix vs prefix operators

    Greetings all. I have what may seem to be a simple question on C++ that I would really like to know the answer to. Years ago I was attending the Software Developers expo in San Francisco, specifically a lecture by Dan Saks (C++ guru). Mr. Saks made an interesting observation that I would like to know the "why" of. Basically he said that using the prefix operator was somehow more efficient than the postfix when incrementing or decrementing numeric variables. Can anyone explain why:

    ++X;

    Is any more efficient/faster/better than:
    X++;

    ??? I have always wondered about this....

    Cheers
    Jeff
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    More accurately, the prefix version will typically be no less efficient than the postfix version, and may be more efficient. The reason is that a typical implementation of the postfix version will involve a temporary to return, whereas the prefix version has no need for such a temporary.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by laserlight View Post
    More accurately, the prefix version will typically be no less efficient than the postfix version, and may be more efficient. The reason is that a typical implementation of the postfix version will involve a temporary to return, whereas the prefix version has no need for such a temporary.
    @LL: Thank you SO much for the reply. This bears out what Dan Saks said in the lecture but he did not explain why and since the topic (metaprogramming) dominated the Q&A afterworlds I never got a chance to ask. For most folks this will make no difference but in the land of embedded coding every clock-cycle counts.


    Thank you again,
    Jeff
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Postfix iterator increment faster than prefix?
    By Optimizinator in forum C++ Programming
    Replies: 8
    Last Post: 06-11-2010, 12:21 PM
  2. Is prefix faster than postfix?
    By dwks in forum C Programming
    Replies: 6
    Last Post: 07-28-2005, 11:51 AM
  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