Thread: postfix vs prefix operators

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 12:48 PM