Ticket #1064 (closed enhancement: fixed)
[PATCH] Performance improvement. It is more effective to use the prefix form of ++iterator (vs iterator++)
| Reported by: | Echelon9 | Owned by: | |
|---|---|---|---|
| Priority: | Should Have | Milestone: | Alpha 9 |
| Component: | Core engine | Keywords: | review, STL |
| Cc: |
Description
An iterator is changed in the program code by the increment/decrement postfix operator. Since the previous iterator's value is not used, you may replace the postfix operator with the prefix one. In some cases, the prefix operator will work faster than the postfix one, especially in Debug-versions.
The prefix increment operator changes the object's state and returns itself already changed.
The situation with the postfix increment operator is more complicated. The object's state must change but it is the previous state which is returned. So an additional temporary object is created.
Reference: Scott Meyers' Rule 6. Distinguish between prefix increment and decrement operators, in:
Meyers, Scott. More Effective C++: 35 New Ways to Improve Your Programs and Designs. Addison-Wesley, Reading, Mass., 1996. ISBN-10: 020163371X. ISBN-13: 9780201633719.
Attachments
Change History
Changed 17 months ago by Echelon9
- Attachment iterator-fix.patch added
comment:2 Changed 17 months ago by Philip
I don't think performance is a good reason for this specific patch, since the affected functions are far from performance bottlenecks and since an optimising compiler can easily figure out how to do std::vector::iterator++ efficiently. But stylistic consistency is nice, and we do ++it much more commonly than it++, so I think the patch is worthwhile for improving consistency.
Changed 17 months ago by Echelon9
- Attachment t1064-additional-profile-iterator-fixes.patch added
Additional two cases missed

Proposed fix for STL iterators prefix/suffix increment