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

which increment is usefull

72
Hi All,

Is any one tell me the difference between i = i + 1 and i++.
Which one do you prefer to use?

Thanks,
Manjunath
Jul 24 '09 #1
7 2098
Markus
6,050 Expert 4TB
They both do the same thing. Personally I prefer the i++ option - much tidier.
Jul 24 '09 #2
Banfa
9,065 Expert Mod 8TB
Also in C++ when working with objects rather than integers i++ is rather more efficient than i = i + 1 and in fact ++i is even more efficient.
Jul 24 '09 #3
After studying little bit of assembly programming in microprocessor I feel that i++ and i+1 differs in the way they are computed at register level.

i++ requires just one register of cpu which stores value of i, and it increments that register.
its assembly code: INC I;

but in case of i+1 it requires two registers one to store i and other to store 1.
its assembly code: ADD I,1;

As far as speed is concerned i dont think they differ at microprocessor level because both operations takes one clock cycle.
Jul 25 '09 #4
newb16
687 512MB
On modern x86 (e.g. netburst ) INC may be slower than ADD - because it doesn't modify carry flag, and if the next op is some ( not any ) conditional branch, cpu will flush the pipeline (so called partial flags stall)
Jul 25 '09 #5
Banfa
9,065 Expert Mod 8TB
Actually any conclusions you draw about C++ language structure based on the assembly (machine code) that you end up with apply only for the specific platform/compiler combination.

Any other compiler may produce different machine code (especially if it has a different optimiser) and any other platform (processor) will have a different instruction set with different characteristics.
Jul 25 '09 #6
If the performance difference between ++i and i++ matters for a project, I am guessing that anyone even remotely competent is going to run a few tests to measure it. It's true that ++i tends to generally be faster on x86 based systems, but you can't really assume that unless you test it on every single x86 CPU out there.

With that said, using either operators will have no notable impact unless all of this is true:
1) 99% or more CPU usage. Not 60%, 80% or 90%.
2) increment is done in a performance critical section multiple times
3) increment cannot be optimized or the compiler will not optimize it
Jul 25 '09 #7
manjuks
72
Hi All,

Thanks for your need full replies.

Thanks,
Manjunath
Jul 27 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a...
29
by: guru.slt | last post by:
"the c++ standard library, tutorial and reference" book, it says: ++i is faster than i++. the latter involves a temporary object because it must return the old value/object of i. for this reason,...
2
by: Tom | last post by:
I am trying to store information into a table that has an auto increment field. There is currently no data in the table. Using the code below I cannot insert data into the table. I get an error...
5
by: Kaush | last post by:
how can i make functions which behaves like printf. i mean the function should be capable of accepting variable number of arguments and of varaible types. thanx kaush
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
3
by: George Ter-Saakov | last post by:
What is the purpose of having Interlocked.Increment if it does not work with variable declared as volatile. Here is my problem, Interlocked.Increment increments the variable in thread safe...
2
by: john | last post by:
Is it true that if I split my access database in backend and frontend and I implement custom auto increment for the ID fields, that my database is ready to be used in a multi-user environment? I...
36
by: lak | last post by:
Which is faster? Post Increment or assignment? Why? I was not able to get any things.
5
by: Nitesh | last post by:
Objects of type X are basic building blocks of my application and there are a few functions which return XHandle (typedef X** XHandle). The task at hand needs to use a couple of these functions and...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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?

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.