473,587 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

i++ or ++i, any difference?

Hi,

Just curious as I'm new to C++ coming from Java background. Is there a
difference in terms of performance if I used i++ instead of ++i in my
codes such as

for(int i=0; i<size; i++)

vs

for(int i=0; i<size; ++i)

I'm under the impression certain compilers like Visual C++ prefers ++i
as it does not create temporaries
(http://www.tantalon.com/pete/cppopt/final.htm).

Thanks in advance for any clarifications!

Regards,
Damon

Jul 23 '05 #1
3 1119

"DamonChong " <so********@exc ite.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Hi,

Just curious as I'm new to C++ coming from Java background. Is there a
difference in terms of performance if I used i++ instead of ++i in my
codes such as

for(int i=0; i<size; i++)

vs

for(int i=0; i<size; ++i)
Probably not, with a built-in type. If the ++ operators are
members of a user defined type with a 'lengthy' construction
process, then the postfix form could be slower, due to the need
to create a temporary object to save the original value. In any
event, the only way to know for sure is to measure (and this
measurement can easily vary among implementations ).
I'm under the impression certain compilers like Visual C++ prefers ++i
as it does not create temporaries
(http://www.tantalon.com/pete/cppopt/final.htm).
I think you're taking the advice at that link out of context.

With a good optimizing compiler (VC++ imo qualifies in this context,
there should be no measurable performance difference between ++i and
i++ with a built-in type.
Thanks in advance for any clarifications!


A final word of advice: Don't worry about such micro-optimizations.
Write your code for clarity and maintainability . Only if you later
can prove that performance is an issue should you consider optimization.
And then, don't try to guess where to do it. Use a profiler.

-Mike
Jul 23 '05 #2
Thanks Mike, I'll keep the advice in mind! Cheers!

Mike Wahler wrote:
"DamonChong " <so********@exc ite.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Hi,

Just curious as I'm new to C++ coming from Java background. Is there a difference in terms of performance if I used i++ instead of ++i in my codes such as

for(int i=0; i<size; i++)

vs

for(int i=0; i<size; ++i)
Probably not, with a built-in type. If the ++ operators are
members of a user defined type with a 'lengthy' construction
process, then the postfix form could be slower, due to the need
to create a temporary object to save the original value. In any
event, the only way to know for sure is to measure (and this
measurement can easily vary among implementations ).
I'm under the impression certain compilers like Visual C++ prefers ++i as it does not create temporaries
(http://www.tantalon.com/pete/cppopt/final.htm).


I think you're taking the advice at that link out of context.

With a good optimizing compiler (VC++ imo qualifies in this context,
there should be no measurable performance difference between ++i and
i++ with a built-in type.
Thanks in advance for any clarifications!


A final word of advice: Don't worry about such micro-optimizations.
Write your code for clarity and maintainability . Only if you later
can prove that performance is an issue should you consider

optimization. And then, don't try to guess where to do it. Use a profiler.

-Mike


Jul 23 '05 #3
There's absolutely no difference in performance if you use it on int or any
other of the built-in types. There might a difference if you try increment
operator on object of user defined class.

cheers,
Marcin

Uzytkownik "DamonChong " <so********@exc ite.com> napisal w wiadomosci
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Hi,

Just curious as I'm new to C++ coming from Java background. Is there a
difference in terms of performance if I used i++ instead of ++i in my
codes such as

for(int i=0; i<size; i++)

vs

for(int i=0; i<size; ++i)

I'm under the impression certain compilers like Visual C++ prefers ++i
as it does not create temporaries
(http://www.tantalon.com/pete/cppopt/final.htm).

Thanks in advance for any clarifications!

Regards,
Damon

Jul 23 '05 #4

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

Similar topics

34
7059
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. Yensao
21
2993
by: b83503104 | last post by:
Hi, Can someone tell me the difference between single quote and double quote? Thanks
26
4390
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: starttime = Date.parse("Aug 10,2003, 07:07") sdt = new Date(starttime)
21
2827
by: Rich | last post by:
I was considering C# for developing a scientific application, but I have noticed a ~30% difference between VC++ .NET and C# on the same machine, under identical conditions: double a = 0,b = 0, c = 0, d = 0, e = 0; for(int n = 0; n != 6000000; n++) { a = n % 5 *2 / 3 - 4 + 6 / 3 - n + n * 2; b = n * 2.3 - 1 *2 / 3 - 4 + 6 / 3 - n + n * 2;...
4
15711
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow which calculated the difference in years and days between two dates, and takes leap years into account? I'm calculating the difference in the usual...
3
4148
by: bbawa1 | last post by:
Hi, I have a table which has a field ItemsReceived of type datetime. I have a grid view which has two columns. In first column i have to show the data from field ItemsReceived and in second column I have to show difference between Currenttime and date from ItemReceived. How can I do that.
12
2705
by: Petronius | last post by:
Hallo, does anyone have an idea how to implement difference lists in Javascript? Thanks allot in advance
5
3478
by: Julius | last post by:
Hej dudes, I need to calc the difference between two timestamps / dates ... For example what i need to calculate: Date 1: 2007.11.06 - 20:13:04 Date 2: 2007.11.07 - 21:13:04 Difference: 1 day, 1hour
9
2664
by: viki1967 | last post by:
Hi all! This new forum its great! :) Congratulations !!! My answer: why this my code not working? Nothing error but not work the difference.... : <html>
11
12097
by: cmb3587 | last post by:
I have two arrays and I'm trying to create a 3rd array that is the difference between the two arrays Ex: arrayA: 3 5 8 9 arrayB: 3 4 6 9 difference of A-B: 5 8 however, my code is just returning me an array of 0's
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.