Blitz logo

Blitz Support :

From: Frank Astier (fastier_at_[hidden])
Date: 2005-05-17 12:45:08


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