Advertisement
maartenk

eigen_benchmark

Nov 28th, 2012
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <Eigen/Dense>
  5. #include "eigen_mematrix.h"
  6. #include "eigen_mematrix.cpp"
  7. #include <time.h>
  8.  
  9. using namespace Eigen;
  10. using namespace std;
  11.  
  12. int main()
  13. {
  14.     int a1, a2, b1, b2;
  15.     a1 = b1 = b2 = 2139;
  16.     a2 = 3;
  17.  
  18.     MatrixXd randomNumbers = MatrixXd::Random(a1, a2);
  19.     mematrix<double> probRandom(a1, a2);
  20.     mematrix<double> tXX;
  21.     probRandom.data=randomNumbers;
  22.     //symMat should be a symmetric matrix since this is a covariance matrix
  23.     MatrixXd symMat = MatrixXd::Ones(b1, b2);
  24.     mematrix<double> probSym(b1,b2);
  25.     probSym.data=symMat;
  26.     MatrixXd newmat;
  27.     int n = 200;
  28.  
  29.     clock_t t1, t2;
  30.     float diff;
  31.     //repeat everything multiple times to exclude other processes interfering
  32.     for (int b = 0; b < 3; b++)
  33.     {
  34.     //eigen native implementation for multiplying
  35.         t1 = clock();
  36.         for (int a = 0; a < n; a++)
  37.         {
  38.             MatrixXd tx = randomNumbers.transpose();
  39.             tx=tx*symMat;
  40.             newmat=tx*randomNumbers;
  41.  
  42.         }
  43.         t2 = clock();
  44.         diff = ((float) t2 - (float) t1) / 1000000.0F;
  45.         printf("eigen mult.  : %.2lf seconds \n", diff);
  46.    
  47.     //make use of interface to eigen implementation for multiplying
  48.         t1 = clock();
  49.         for (int a = 0; a < n; a++)
  50.         {
  51.             newmat=probRandom.data.transpose()*probSym.data*probRandom.data;
  52.         }
  53.        t2 = clock();
  54.        diff = ((float) t2 - (float) t1) / 1000000.0F;
  55.        printf("probabel mult: %.2lf seconds \n", diff);
  56.  
  57.        //eigen native implementation for setting values
  58.        double putvalue=12.34;
  59.        t1 = clock();
  60.        for (int a = 0; a < n; a++)
  61.        {
  62.            for (int i = 0; i < b1; i++){
  63.                for (int j = 0; j < b2; j++){
  64.                    probSym.put(putvalue,i,j);
  65.                }
  66.            }
  67.        }
  68.        
  69.      t2 = clock();
  70.      diff = ((float) t2 - (float) t1) / 1000000.0F;
  71.      printf("probabel : put  %.2lf seconds \n", diff);
  72.  
  73.      //make use of interface to eigen implementation for setting values
  74.     t1 = clock();
  75.        for (int a = 0; a < n; a++)
  76.        {
  77.            for (int i = 0; i < b1; i++){
  78.                for (int j = 0; j < b2; j++){
  79.                    symMat(i,j)=putvalue;
  80.                }
  81.            }
  82.  
  83.        }
  84.        t2 = clock();
  85.        diff = ((float) t2 - (float) t1) / 1000000.0F;
  86.        printf("eigen : put  %.2lf seconds \n", diff);
  87.  
  88.     }//end loop over all functions
  89.  
  90.  
  91. }//end of main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement