On Wed, Jul 7, 2010 at 4:45 PM, Max S. Kaznady
Great example, thanks! Before your post, I wrote something like:
namespace my { void my_vec_pow(boost::numeric::ublas::vector<double> &vec, double expon) { boost::numeric::ublas::vector<double>::iterator it; for (it = vec.begin(); it != vec.end(); ++it) *it = std::pow(*it, expon); } }
I'm just curious, but from performance perspective, how much more efficient is it to use iterators and functors? I would assume that most compilers can exploit this to improve memory management and speed up the computation... I'll write some benchmarks later on this week, efficiency is crucial for what I'm doing.
By and large, in my experience, it's barely measurable, at least for simple cases. When you write for(i=v.begin(); i!=v.end();....) v.end() is evaluated on each iteration, which may have some effect, depending on type of v, but for simple vectors it's probably inlined anyway. (there's folks on here that know much more about it than I do). With the new Boost.Range algorithms there's also some syntactic simplification as you can write for(v,fn); which is a real plus if v is complicated expression, since you don't have to evaluate it twice or even put it in an explicit variable. Cheers - Rob.