473,394 Members | 1,749 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,394 software developers and data experts.

post vs. pre incrementation

Hi!

I have a very simple question: Is it still true that pre variable
incrementation (like ++i) is faster than post incrementation (i++). I heard
that in modern compilers it makes no difference as the both versions are
optimised to one type of code. I've also never encountered any performance
difference in using e.g.

for(i=0; i < n; ++i) statement;

over

for(i=0; i < n; i++) statement;

However, nearly every methodology suggest using the first case.

Could you explain me why it is so, and if it makes any difference for VC++
or VC.NET?

Regards,

Michael
Nov 17 '05 #1
2 3873
It doesn't for integers. It can for more complex types because for these you
need to make a copy in the post-increment case and you don't need to in the
pre-increment case. Looking at the generated code for a more complex type
like a non pointer iterator should illustrate the difference.

Ronald Laeremans
Visual C++ team

"Michael B." <mi*****@3-e.pl> wrote in message
news:O6**************@TK2MSFTNGP09.phx.gbl...
Hi!

I have a very simple question: Is it still true that pre variable
incrementation (like ++i) is faster than post incrementation (i++). I heard that in modern compilers it makes no difference as the both versions are
optimised to one type of code. I've also never encountered any performance
difference in using e.g.

for(i=0; i < n; ++i) statement;

over

for(i=0; i < n; i++) statement;

However, nearly every methodology suggest using the first case.

Could you explain me why it is so, and if it makes any difference for VC++
or VC.NET?

Regards,

Michael

Nov 17 '05 #2
Michael B. <mi*****@3-e.pl> wrote:
[...] I've also never encountered any performance
difference in using e.g.

for(i=0; i < n; ++i) statement;

over

for(i=0; i < n; i++) statement;
Which types did you test this with?
I would expect this to be cured by
the optimizer.
However, nearly every methodology suggest using the first case.

Could you explain me why it is so, and if it makes any difference for VC++
or VC.NET?
Here is a canonical implementation for
the two for a user-defined type 'T':

T& operator++() {inc();return *this;}
T operator++(int) {T tmp(*this);inc();return tmp;}

You see that the postfix version calls
the copy ctor two times. It depends on
the actual code whether the optimizer
can remove this.
This is why I would recommend you always
do what you want: If you need the old
value, use postfix increment(/decrement).
Otherwise, use the prefix one.
Regards,

Michael


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #3

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

Similar topics

15
by: Thomas Scheiderich | last post by:
I am trying to understand Session variables and ran into a question on how they work with data that is passed. I have an HTM file that calls an ASP file and sends the name either by GET or POST....
13
by: kailasam | last post by:
Hello, Iam having a doubt. Which is more efficient post increment or Pre increment? I have read that preincrement is efficient than Post increment. Iam not able to think how it is? For an...
1
by: khawar | last post by:
my application is in asp.net using C# hi guys having a complicated problem i am using payflowlink to process CC payments I have to send a httppost to their servers. The problem is how do i do a...
2
by: Matt | last post by:
When we submit the form data to another page, we usually do the following: <form action="display.aspx" method="post"> will submit the form data and open display.asp in the current browser ...
1
by: Manuel | last post by:
I have to log into a website and retrieve some information. The problem is that the post isn't "normal". I'm used to passing post values in the form of: Variable1=Value1&Variable2=Value2 etc. I...
11
by: divya_rathore_ | last post by:
The code: int aaa = 100; printf("%d %d %d\n", --aaa, aaa, aaa--); printf("%d\n", aaa); prints: 99 100 100 98
4
by: sam | last post by:
hI, I am little confused here See i have int wordlen=10; when int s is array s++; whats the meaning of this
2
by: sanctus | last post by:
what is the difference between for(j=0;j<cl.ts->size();++j){ double l=cmbcalc->j2l; } and
6
Dormilich
by: Dormilich | last post by:
Hi, I’ve got a mathematical script, which is currently running fine*, although it bothers me that JS does not always increment correctly. sometimes it happens that the incremented value is...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.