-
July 19th, 2005, 07:58 AM
#1
Re: C++ language: Why should I use ++i instead of i++ ?
[Andreas]: The following question is asked in regard to the following FAQ:
You mention that for regular variables (like int) there is no performance gain. Is this statement true for C++ but not for C? I've come across several people who do ++i all the time rather than i++ (in C) claiming efficiency gains.
Last edited by Andreas Masur; July 29th, 2005 at 12:42 PM.
-
July 29th, 2005, 12:42 PM
#2
Re: Why should I use '++i' instead of 'i++'?
-
July 30th, 2005, 01:18 AM
#3
Re: C++ language: Why should I use ++i instead of i++ ?
Originally Posted by C++ Newbie
[Andreas]: The following question is asked in regard to the following FAQ:
You mention that for regular variables (like int) there is no performance gain. Is this statement true for C++ but not for C? I've come across several people who do ++i all the time rather than i++ (in C) claiming efficiency gains.
normally it's a question of whether your compiler is able to "take a look" inside operator's implementation and optimise away a temporary variable if it's really not needed (not used by the caller code). for native compiler types it's possible and, more to say, with a clever compiler it's even possible with a user defined type under some circumstances.
e.g. when class's operator++ (int) is defined with "inline" specificator.
the recent c compilers do this optimization (at least, I have checked it out with gcc 3.3.4). what's about using ++i instead of i++? I guess it becomes a matter of habbit with time. e.g. if I know that I donot need an old value of the variable, I just don't use i++ even for for-loops.
"UNIX is simple; it just takes a genius to understand its simplicity!"
-
July 30th, 2005, 03:25 AM
#4
Re: C++ language: Why should I use ++i instead of i++ ?
Personally, I think ++i reads better: "increment i", rather than i++ - "i increment".
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 30th, 2005, 03:44 AM
#5
Re: C++ language: Why should I use ++i instead of i++ ?
Originally Posted by C++ Newbie
[Andreas]: The following question is asked in regard to the following FAQ:
You mention that for regular variables (like int) there is no performance gain. Is this statement true for C++ but not for C? I've come across several people who do ++i all the time rather than i++ (in C) claiming efficiency gains.
Since C does not allow to overload operator ++, you can only use this operator on builtins types, so the performances of prefix and postfix versions are basically identical (at least if the return value is not used, the assembly code generated will be the same).
But, it is better to use ++i
- It is a good habit to use ++i, so you don't have to think about what version you use, and you will not accidentally use i++ where you should have used ++i
- It is easier for the programmer's mind, to don't matter which operator to use.
- If a day, you want to replace your C-style pointers to array by iterators of a STL container, you will need less code modification for optimal performances.
Note that for an easy conversion, it is generally good to typedef pointers that you use much, so you can replace them by iterators by changing a single line of code. - As Graham said, ++i can be literally read "increment i" which is maybe more intuitive than i++. But for an advanced programmer i don't think it makes any difference.
I don't understand why books i read used i++ everywhere, so, during a long time, i had the bad habit to use i++.
But i think that it is far better to get the good habits (especially if they are easy to take) from the begining of C++ learning.
-
July 30th, 2005, 06:10 AM
#6
Re: C++ language: Why should I use ++i instead of i++ ?
I also use i++ because of habbit over the past 20 or so years and never use ++i. Also use it on c++ iterators in for loops and for most other pointers. None of my programs are time-critical so speed difference of a couple clock ticks is unimportant.
-
July 30th, 2005, 06:52 AM
#7
Re: Why should I use '++i' instead of 'i++'?
++i as know as PreIncrement
and i++ as post increment
if you consider then in a loop
like for or something else
there is no difference between ++ior i++
but as we know ++i first increment the value by one then print
but i++ first print the value then increment.
means i++ uses more cpu cycle.
so it's upto you which one you want to use .most people like to use i++.becuase from the start onwards they think or they start write like this only i++ means increment the value of i by 1.
they think ++i at the time when they have to use preincrement operator
so better to use ++i.to minimize your cpu cycle.and improve your speed
-
July 30th, 2005, 07:29 AM
#8
Re: Why should I use '++i' instead of 'i++'?
Originally Posted by humptydumpty
but as we know ++i first increment the value by one then print
but i++ first print the value then increment.
Print? Print what? Did you mean evaluate?
Originally Posted by Graham
Personally, I think ++i reads better: "increment i", rather than i++ - "i increment".
Personally, I think the opposite.
-
July 30th, 2005, 07:34 AM
#9
Re: Why should I use '++i' instead of 'i++'?
Originally Posted by humptydumpty
so better to use ++i.to minimize your cpu cycle.and improve your speed
I might agree with that in an application that does that millions of times a second -- but in other applications it doesn't make a hoot which you use. Whether you use ++i or i++ is merly a personal decision, beyond that it is a moot question. Who the he11 cares whether i++ takes 1 or 2 clock ticks more than ++i??? Your time and efforts to optimize a program are much better spent elsewhere. [edit] you might care if you are compiling for a very sloooooooooow computer[/edit]
Last edited by stober; July 30th, 2005 at 07:39 AM.
-
July 30th, 2005, 08:24 AM
#10
Re: Why should I use '++i' instead of 'i++'?
Originally Posted by stober
I might agree with that in an application that does that millions of times a second -- but in other applications it doesn't make a hoot which you use. Whether you use ++i or i++ is merly a personal decision, beyond that it is a moot question. Who the he11 cares whether i++ takes 1 or 2 clock ticks more than ++i??? Your time and efforts to optimize a program are much better spent elsewhere. [edit] you might care if you are compiling for a very sloooooooooow computer[/edit]
I don't agree.
- It is better that all programmers use the same convention.
So why not use preincrement, since it is a little better? - Generally preincrement and postincrement have the same performance, but for iterators as deque::iterator, the difference not negligible.
For example, i found that for a copy from deque to deque, using postincrement is more than two times slower than preincrement, with my computer, and my STL.
For complex algorithms this may be not an issue, if much processing is done between to calls to the increment iterator.
But, since STL is everywhere nowadays, it is probable that for some actual programs, and some future programs, the CPU time executing various increment operators on various iterators of various containers, may use 50% of CPU cycles.
In fact when using deque, generally dereferencing is fact, but incrementation is slow, so the incrementation may take 75% CPU cycles on some algorithms using deque on a simple data structure (deque<char> for example).
If you replace only one postincrement by one preincrement in your whole code, you will probably not notify the difference, but if you replace them all, it can make a big difference. - Even if the two things are nearly identical, why not using the best one?
Don't say that it is easier to write "i++" than "++i", for the programmer learning C++, there is no difference choosing one or the other, so it is better to get the good habit from the start.
-
July 30th, 2005, 08:43 AM
#11
Re: Why should I use '++i' instead of 'i++'?
Originally Posted by SuperKoko
I don't agree.
Everyone is entitled to his/her own opinion
Originally Posted by SuperKoko
- It is better that all programmers use the same convention.
That will surly be a very cold day in he11 There are as many ways to write a program as there are programmers to write them. But company coding standards (and I hope most software houses have them) should standardize most of that.
Originally Posted by SuperKoko
- Generally preincrement and postincrement have the same performance, but for iterators as deque::iterator, the difference not negligible.
For example, i found that for a copy from deque to deque, using postincrement is more than two times slower than preincrement, with my computer, and my STL
I'll accpet your opinion and experience on that. It never occurred to me to use pre-increment on iterators because all the text I read used post-increment. I assumed (maybe wrongly) that iterators were just another kind of pointer, and pre-post increment debate would apply to them as it does to integers.
BTW: Thanks for showing me how to use the [ list ] HTML tags -- I've been woundering how those bullets were made.
Last edited by stober; July 30th, 2005 at 08:49 AM.
-
July 30th, 2005, 09:33 AM
#12
Re: Why should I use '++i' instead of 'i++'?
After this long discussion on the preferences based on performance, I would add up a thing.
This depends on the way your need for the incrementation is. I mean if you need the incremented value of a variable in the statement where you are doing you might want to use ++i and if you want the older value of the variable in the statement where you use ++ and want that it should have an incremented value later use i++. Basically what I am talking about is related to the actual meaning of post and pre increment. Performance reasons are always there but you should never neglect the actual objective of your code.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
July 30th, 2005, 12:20 PM
#13
Re: Why should I use '++i' instead of 'i++'?
I think some sample code use showing the difference between "preincremented" and "postincremented" will help here:
int i=10;
cout<<i++<<endl;
cout<<i<<endl;
int d=10
cout<<++d<<endl;
cout<<d<<endl;
output:
10
11
11
11
-
July 31st, 2005, 02:26 AM
#14
Re: Why should I use '++i' instead of 'i++'?
I use ++i everytime that I don't need to increment after using the variable. It just seems better.
-
July 31st, 2005, 01:05 PM
#15
Re: Why should I use '++i' instead of 'i++'?
I prefer to use ++i but I did come from a C background originally and i++ was far more popular (perhaps why the language is called C++ and not ++C). Perhaps because i++ is shorthand for i+=1 (at least it appears to be) where the operation on i is on the right-hand side. You cannot do 1+=i (1 is not an l-value) or even 1=+i (same error).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|