![]() |
Blitz Support : |
From: Julian Cummings (cummings_at_[hidden])
Date: 2005-05-17 15:15:08
One little postscript to my earlier message. The Vector class doesn't
actually have a mean() function defined for it, but it does have sum()
defined and there is a length() method to get the number of elements.
So it is very similar to the STL vector class in this regard, and the
performance is virtually identical as you would expect.
-- Julian C.
On Tue, 2005-05-17 at 12:56 -0700, Julian Cummings wrote:
> Hi Frank,
>
> The overhead you are seeing with the blitz Array is due to the fact that
> Arrays can have multiple dimensions. The reduction code in the header
> file <blitz/array/reduce.cc> does some extra work to loop over the Array
> indices for each dimension.
>
> If you replace Array<double,1> with Vector<double> and use the blitz 1D
> Vector container, you should see this overhead disappear. I get the
> same timing for the blitz Vector and the STL vector class. (BTW, I had
> to do some major surgery on your code example to get it to compile, and
> I changed the expression used for filling the vector elements to avoid
> producing NaNs and a result of "inf" for the mean.)
>
> It is hard to eliminate this sort of overhead without introducing a new
> container type such as Vector because you can't have reduction functions
> that are partially specialized for N_rank=1.
>
> Regards, Julian C.
>
>
> On Tue, 2005-05-17 at 10:45 -0700, Frank Astier wrote:
> > Hi -
> >
> > I'm trying to use Blitz for a very simple mean computation on a 1D
> > vector of floats. I'm comparing to my own algorithm, which just does
> > a linear scan with N additions and a division. Compiling with -O2,
> > apparently, Blitz is slower... Am I missing some compilation flag to
> > make the thing go faster?
> >
> > Here are the runtime results and the whole program, followed by my
> > implementation of mean (admittedly not safe):
> >
> > Blitz: N = 5000000 mean = inf in 1.71s
> > Utils: N = 5000000 mean = inf in 1.6s
> >
> > int main(int, char** argv) {
> >
> > const size_t N = 5000000;
> >
> > {
> > Array<double, 1> a(N);
> >
> > for (size_t i = 0; i < N; ++i)
> > a(i) = double(rand() % 32768) / double(rand() % 65535);
> >
> > timer t;
> > double m1 = mean(a);
> > cout << "Blitz: N = " << N << " mean = " << m1
> > << " in " << t.elapsed() << endl;
> > }
> >
> > {
> > vector<double> a(N);
> >
> > for (size_t i = 0; i < N; ++i)
> > a[i] = double(rand() % 65535) / double(rand() % 32768);
> >
> > timer t;
> > double m1 = mean(a);
> > cout << "Utils: N = " << N << " mean = " << m1
> > << " in " << t.elapsed() << endl;
> > }
> >
> > return 0;
> > }
> > //------------------------------------------------------------------------------
> > template <typename Iter1>
> > inline IteratorValueType mean(Iter1 first, Iter1 last)
> > {
> > // non empty ranges only
> > return sum(first, last) / (last - first);
> > }
> >
> >
> > //------------------------------------------------------------------------------
> > template <typename Container>
> > inline ContainerValueType mean(const Container& v)
> > {
> > return mean(v.begin(), v.end());
> > }
> >
> > Frank
> >
> >
> > _______________________________________________
> > Blitz-support mailing list
> > Blitz-support_at_[hidden]
> > http://www.oonumerics.org/mailman/listinfo.cgi/blitz-support
> >