src/work/alpar/matrix.cc
changeset 1063 d53a12d49262
equal deleted inserted replaced
-1:000000000000 0:637c6f2cbdd0
       
     1 #include <lemon/time_measure.h>
       
     2 #include <vector>
       
     3 
       
     4 template<class T>
       
     5 class Matrix 
       
     6 {
       
     7 public:
       
     8   int m,n;
       
     9   std::vector<T> values;
       
    10   
       
    11   Matrix(int _m, int _n) : m(_m), n(_n), values(n*m) {}
       
    12   typename std::vector<T>::reference operator()(int i, int j)
       
    13   { return values[i*n+j];}
       
    14 
       
    15   class Row 
       
    16   {
       
    17   public:
       
    18     Matrix<T> &mat;
       
    19     int row;
       
    20     
       
    21     Row(Matrix<T> &_mat,int _row) : mat(_mat), row(_row) {}
       
    22     typename std::vector<T>::reference operator()(int j)
       
    23     { return mat.values[row*mat.n+j];}
       
    24   };
       
    25   class Column 
       
    26   {
       
    27   public:
       
    28     Matrix<T> &mat;
       
    29     int col;
       
    30     
       
    31     Column(Matrix<T> &_mat,int _col) : mat(_mat), col(_col) {}
       
    32     typename std::vector<T>::reference operator[](int i)
       
    33     { return mat.values[i*mat.n+col];}
       
    34   };
       
    35   
       
    36   Row operator[](int i) { return Row(*this,i); }
       
    37   Column operator()(int j) { return Column(*this,j); }
       
    38 };
       
    39 
       
    40 main() 
       
    41 {
       
    42   const int COUNT=1000000;
       
    43   const int SIZE=100;
       
    44   
       
    45   Matrix<int> A(SIZE,SIZE);
       
    46   
       
    47   for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) A(i,j)=i+j;
       
    48 
       
    49   int s=0;
       
    50   lemon::Timer T;
       
    51   for(int c=0;c<COUNT;c++) {
       
    52     for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) s+=A[i](j);
       
    53   }
       
    54   std::cout << "A[i](j): " << T << ' ' << s <<'\n';
       
    55   s=0;
       
    56   T.reset();
       
    57   for(int c=0;c<COUNT;c++) {
       
    58     for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) s+=A(i,j);
       
    59   }
       
    60   std::cout << "A(i,j): " << T << ' ' << s << '\n';
       
    61   s=0;
       
    62   T.reset();
       
    63   for(int c=0;c<COUNT;c++) {
       
    64     for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) s+=A(j)[i];
       
    65   }
       
    66   std::cout << "A(j)[i]: " << T << ' ' << s << '\n';
       
    67   s=0;
       
    68   T.reset();
       
    69   for(int c=0;c<COUNT;c++) {
       
    70     for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) s+=A(i,j);
       
    71   }
       
    72   std::cout << "A(j,i): " << T << ' ' << s << "\n\n";
       
    73 
       
    74   return 0;
       
    75 }