Matrix interface running time test...
authoralpar
Mon, 15 Nov 2004 15:05:00 +0000
changeset 994062f98d96f40
parent 993 21d1b4fa1b24
child 995 e5216825f71b
Matrix interface running time test...
src/work/alpar/matrix.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/alpar/matrix.cc	Mon Nov 15 15:05:00 2004 +0000
     1.3 @@ -0,0 +1,75 @@
     1.4 +#include <lemon/time_measure.h>
     1.5 +#include <vector>
     1.6 +
     1.7 +template<class T>
     1.8 +class Matrix 
     1.9 +{
    1.10 +public:
    1.11 +  int m,n;
    1.12 +  std::vector<T> values;
    1.13 +  
    1.14 +  Matrix(int _m, int _n) : m(_m), n(_n), values(n*m) {}
    1.15 +  typename std::vector<T>::reference operator()(int i, int j)
    1.16 +  { return values[i*n+j];}
    1.17 +
    1.18 +  class Row 
    1.19 +  {
    1.20 +  public:
    1.21 +    Matrix<T> &mat;
    1.22 +    int row;
    1.23 +    
    1.24 +    Row(Matrix<T> &_mat,int _row) : mat(_mat), row(_row) {}
    1.25 +    typename std::vector<T>::reference operator()(int j)
    1.26 +    { return mat.values[row*mat.n+j];}
    1.27 +  };
    1.28 +  class Column 
    1.29 +  {
    1.30 +  public:
    1.31 +    Matrix<T> &mat;
    1.32 +    int col;
    1.33 +    
    1.34 +    Column(Matrix<T> &_mat,int _col) : mat(_mat), col(_col) {}
    1.35 +    typename std::vector<T>::reference operator[](int i)
    1.36 +    { return mat.values[i*mat.n+col];}
    1.37 +  };
    1.38 +  
    1.39 +  Row operator[](int i) { return Row(*this,i); }
    1.40 +  Column operator()(int j) { return Column(*this,j); }
    1.41 +};
    1.42 +
    1.43 +main() 
    1.44 +{
    1.45 +  const int COUNT=1000000;
    1.46 +  const int SIZE=100;
    1.47 +  
    1.48 +  Matrix<int> A(SIZE,SIZE);
    1.49 +  
    1.50 +  for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) A(i,j)=i+j;
    1.51 +
    1.52 +  int s=0;
    1.53 +  lemon::Timer T;
    1.54 +  for(int c=0;c<COUNT;c++) {
    1.55 +    for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) s+=A[i](j);
    1.56 +  }
    1.57 +  std::cout << "A[i](j): " << T << ' ' << s <<'\n';
    1.58 +  s=0;
    1.59 +  T.reset();
    1.60 +  for(int c=0;c<COUNT;c++) {
    1.61 +    for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) s+=A(i,j);
    1.62 +  }
    1.63 +  std::cout << "A(i,j): " << T << ' ' << s << '\n';
    1.64 +  s=0;
    1.65 +  T.reset();
    1.66 +  for(int c=0;c<COUNT;c++) {
    1.67 +    for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) s+=A(j)[i];
    1.68 +  }
    1.69 +  std::cout << "A(j)[i]: " << T << ' ' << s << '\n';
    1.70 +  s=0;
    1.71 +  T.reset();
    1.72 +  for(int c=0;c<COUNT;c++) {
    1.73 +    for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) s+=A(i,j);
    1.74 +  }
    1.75 +  std::cout << "A(j,i): " << T << ' ' << s << "\n\n";
    1.76 +
    1.77 +  return 0;
    1.78 +}