#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: | STL |
Cc: | Patch: |
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 (2)
Change History (8)
by , 12 years ago
Attachment: | iterator-fix.patch added |
---|
comment:1 by , 12 years ago
Milestone: | Alpha 8 → Alpha 9 |
---|
comment:2 by , 12 years ago
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.
comment:3 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
by , 12 years ago
Attachment: | t1064-additional-profile-iterator-fixes.patch added |
---|
Additional two cases missed
comment:6 by , 8 years ago
Keywords: | review removed |
---|
Proposed fix for STL iterators prefix/suffix increment