Performance difference between ++iterator and iterator++?

后端 未结 7 1064
悲哀的现实
悲哀的现实 2020-12-08 04:32

My workmate claims that for object types preincrement is more efficient than post increment

e.g.

std::vector vec;

... insert a wh         


        
相关标签:
7条回答
  • 2020-12-08 05:08

    I know this is like "hangar-flying" in aviation. I'm sure it is purely academic, and you know if it makes a hoot of difference you need to be re-thinking your code.

    Example: I got a comment on some answer I gave to some question about performance tuning, and it went like this:

    Person: I tried your method of randomly halting and looking at the call stack. I did it several times, but it makes no sense. All the time, it is deep in some iterator-increment code. What good is that?

    Me: That's great! It means nearly all your time is going into incrementing iterators. Just look higher up the stack and see where it is coming from. Replace that with simple integer-increment, and you'll get a massive speedup.

    Of course, you might prefer the prettiness of iterator-incrementing, but it's easy enough to find out if it's costing you much performance.

    0 讨论(0)
  • 2020-12-08 05:09

    The default increment operators will look like this:

    class X
    {
        X operator++(int)  // Post increment
        {
            X  tmp(*this);
            this->operator++(); // Call pre-increment on this.
            return tmp;
        }
        X& operator++()  // Pre increment
        {
            // Do operation for increment.
            return *this;
        }
    };
    

    Notice the Post-increment is defined in terms of the Pre-increment. So Post-increment does the same work plus some extra. Usually this is construct a copy and then return the copy via a copy (Note the Pre-increment can return itself by reference but the Post-increment can not).

    0 讨论(0)
  • 2020-12-08 05:12

    have a look at this http://discuss.fogcreek.com/joelonsoftware/default.asp?cmd=show&ixPost=171881

    0 讨论(0)
  • 2020-12-08 05:24

    I have read that it makes no difference with compilers that have a decent optimizer. Since the overall loop logic is the same for both, it makes sense to use a pre increment as a good programming habit. That way, if the programmer encounters the "suboptimal" compiler, the best outcome is assured.

    0 讨论(0)
  • 2020-12-08 05:25

    Seriously, unless you're actually having performance problems due to calling post-increment instead of pre-increment THE PERFORMANCE DIFFERENCE WILL ONLY MATTER IN VERY RARE CIRCUMSTANCES.


    Update

    For instance, if your software is a climate simulation, and operator++ causes the simulation to move forward a step (that is ++simulation gives you the next step in the simulation, while simulation++ makes a copy of the current step in the simulation, advances the simulation, and then hands you the copy) then performance will be an issue.

    More realistically, in a comment, the original questioner mentions that we're discussing an embedded project with (apparently) real time requirements. In that case you need to actually look at your specific implementation. I seriously doubt that you'll have noticeable slowdowns iterating through a std::vector with post-increment instead of pre-increment, although I haven't benchmarked any STL implementation on this matter.


    Don't pick one over the other for performance reasons. Pick one over the other for clarity/maintenance reasons. Personally I default to pre-increment because I generally don't give a damn what the value was before incrementing.

    If your brain blows up when you see ++i instead of i++ it may be time to consider going into a different line of work.

    0 讨论(0)
  • 2020-12-08 05:29

    Postincrement must return the value the iterator had BEFORE it was incrementing; so, that previous value needs to be copied somewhere before altering it with the increment proper, so it's available to return. The extra work may be a little or a lot, but it certainly can't be less than zero, compared to a preincrement, which can simply perform the incrementing and then return the just-altered value -- no copying // saving // etc necessary.

    So, unless you specifically MUST have postincrement (because you're using the "value before increment" in some way), you should always use preincrement instead.

    0 讨论(0)
提交回复
热议问题