473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

prefix vs. postfix

Hi,

is there a difference in terms of efficiency
when using a prefix "++x;" instead of a
postfix "x++;"?

Thanks
Chris
Jul 23 '05 #1
2 3610
Christian Christmann wrote:
Hi,

is there a difference in terms of efficiency
when using a prefix "++x;" instead of a
postfix "x++;"?


Depends what the type of x is and what the rest of the
expression is. With built in types, the difference is
negligable to non-existant as the compilers are pretty
good about optimizing either case. In the case of
user defined operator++ functions, it depends on the
definition of the function, but usually the x++ case
returns a copy of the object and ++x returns a reference
so the former is less efficient.
Jul 23 '05 #2
Christian Christmann wrote:
Hi,

is there a difference in terms of efficiency
when using a prefix "++x;" instead of a
postfix "x++;"?


Depends on the type of x and possibly on the optimization capabilities of
the compiler. For built-in types, it's very likely that there is no
difference between prefix and postfix. If it's a user-defined type, then
postfix ++ needs to copy the object first, then increment the original,
then return the copy. So you get the overhead of a copy of the object. If
you don't need the old value, that copy is discarded, and so the code would
be less efficient (unless the compiler is able to optimize that away).
Prefix ++ OTOH just needs to increment the object and then return a
reference to it. No copy is needed.
That's why prefix ++ is preferred in C++ whenever there is no reason to use
postfix instead, and for the sake of consistency, many programmers do it
not only for user-defined types, but also for built-in types.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sergey | last post by:
Hello! Could anybody be kind enough to explain this concept? Why C++ make two ops for prefix and postfix ++ operator? As I guess, it is possible to implement both cases via sole prefix...
8
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
98
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
2
by: shan | last post by:
Hi to everybody, I am begginer in C programming language. My simple doubt is the difference between postfix & prefix unary operator plus. (i.e) i++ and ++i . plz give me an example program...
30
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”....
8
by: subramanian100in | last post by:
Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference
2
by: swapnaoe | last post by:
Hi, In http://elearning.embnet.org/file.php/43/ctutorial/Postfix-and-prefix----and---.html I read about postfix and prefix unary operators. It said " If the increment or decrement operator is...
2
by: puzzlecracker | last post by:
How can we implement operator ++ as postfix in prefix? thanks
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.