Boost logo

Boost Users :

Subject: Re: [Boost-users] apply std::pow to vector or matrix
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2010-07-07 12:13:48


On Wed, Jul 7, 2010 at 4:45 PM, Max S. Kaznady <max.kaznady_at_[hidden]>wrote:

> 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.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net