Performance of pIter != cont.end() in for loop

后端 未结 7 436
面向向阳花
面向向阳花 2021-01-07 18:11

I was getting through \"Exceptional C++\" by Herb Sutter lately, and I have serious doubts about a particular recommendation he gives in Item 6 - Temporary Objects.

7条回答
  •  攒了一身酷
    2021-01-07 18:30

    A couple of things... the first is that in general the cost of building an iterator (in Release mode, unchecked allocators) is minimal. They are usually wrappers around a pointer. With checked allocators (default in VS) you might have some cost, but if you really need the performance, after testing rebuild with unchecked allocators.

    The code need not be as ugly as what you posted:

    for (list::const_iterator it=emps.begin(), end=emps.end(); 
                                        it != end; ++it )
    

    The main decision on whether you want to use one or the other approaches should be in terms of what operations are being applied to the container. If the container might be changing it's size then you might want to recompute the end iterator in each iteration. If not, you can just precompute once and reuse as in the code above.

提交回复
热议问题