Steven Schveighoffer wrote:
On Sun, 26 Apr 2009 21:20:32 -0400, Andrei Alexandrescu
I think, all other things being equal, that opApply tends to be slower than iterators.

It depends. The range must have either inline-able functions, or must NOT call virtual functions. Otherwise, it could be worse. there is also the factor that you are using an inner function with opApply, so if you regularly access variables outside the foreach loop, then those add up.

Observe that in opapply style, you do one delegate call per loop iteration.
With range style, you do three calls on the range per loop iteration (empty, front, popFront).

If those range calls call virtual calls or ARE virtual calls, then range loses. Of course, the difference might be washed if you do lots of accesses to an outer variable in your foreach loop, which require a pointer dereference vs. a stack dereference.

We should quantify this and see what the actual performance is.

This is a very good point. If a range offers virtual empty/popFront/front, then opApply is a faster proposition. This makes for a good use case for making opApply the preferred way of iteration, if present (if you just do foreach and opApply is present, use it).

Andrei

Reply via email to