|
-
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.
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
|