[eigen] what do you think of this simple benchmark

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


I am given a 2 hour short course on cpp classes and operator overloading in 6 weeks with some physics students and want to use a matrix class for that purpose. They will be working with matrixes in a following course anyway. There I have no influence on the class they use, but they should know about the good and bad choices.

The simple matrix class can be seen here:
http://www.c-plusplus.de/forum/viewtopic-var-t-is-230960.html
(comments welcome).

Now I did the following benchmark, which shows clearly how eigen outperformes my own class (not surprising).
Do you have any remarks about this benchmark?

void MatrixTest::benchmark()
{
   int rows = 4;
   int cols = 4;

   matrix a(rows,cols);
   matrix b(rows,cols);
   matrix c(rows,cols);
   matrix d(rows,cols);

   int count = 0;
   for(int i=0; i< rows; ++i)
   {
       for(int j=0; j< cols; ++j)
       {
           count++;
           a.data()[i][j] = count;
           b.data()[i][j] = count + rows*cols;
           c.data()[i][j] = i*j;
       }
   }

   cout << "Matrix 'a'" << endl << a << endl;
   cout << "Matrix 'b'" << endl << b << endl;
   cout << "Matrix 'c'" << endl << c << endl;

   cout << "\n\n matrix (IQO) class\n" << endl;

QTime time; int iterations = 1000;
   for (int j=0; j < 3; ++j)
   {
       time.start();
       for (int i=0; i < iterations; ++i)
       {
d = (a*b + a*c + b*c)*a + (a*b + a*c + b*c)*b + (a*b + a*c + b*c)*c;
       }
cout << "Time for:" << iterations << " iterations: " << time.elapsed() << " ms" << endl;
   }
Matrix4f m1, m2, m3, m4;
   //m1 << 1, 2, 3, 4,
   //      5, 6, 7, 8,
   //      9, 10, 11, 12,
// 13, 14, 15, 16;
   for(int i=0; i< rows; ++i)
   {
       for(int j=0; j< cols; ++j)
       {
           m1(i,j) = a.data()[i][j];
           m2(i,j) = b.data()[i][j];
           m3(i,j) = c.data()[i][j];
       }
   }

//std::cout << "m1\n" << m1 << "\nm2:\n" << m2 << "\nm3:\n" << m3 << std::endl;

   cout << "\n Eigen (fixed size)\n" << endl;
   for (int j=0; j < 3; ++j)
   {
       time.start();
       for (int i=0; i < iterations*100; ++i)
       {
m4 = (m1*m2 + m1*m3 + m2*m3)*m1 + (m1*m2 + m1*m3 + m2*m3)*m2 + (m1*m2 + m1*m3 + m2*m3)*m3;
       }
cout << "Time for:" << iterations << " iterations: " << time.elapsed()/100.0 << " ms" << endl;
   }
MatrixXi ma(rows,cols);
   MatrixXi mb(rows,cols);
   MatrixXi mc(rows,cols);
   MatrixXi md(rows,cols);

   for(int i=0; i< rows; ++i)
   {
       for(int j=0; j< cols; ++j)
       {
           ma(i,j) = a.data()[i][j];
           mb(i,j) = b.data()[i][j];
           mc(i,j) = c.data()[i][j];
       }
   }

   cout << "\n Eigen (dynamic size)\n" << endl;
   for (int j=0; j < 3; ++j)
   {
       time.start();
       for (int i=0; i < iterations; ++i)
       {
md = (ma*mb + ma*mc + mb*mc)*ma + (ma*mb + ma*mc + mb*mc)*mb + (ma*mb + ma*mc + mb*mc)*mc;
       }
cout << "Time for:" << iterations << " iterations: " << time.elapsed() << " ms" << endl;
   }

   std::cout << "\n\nMatrix 'd'\n"  << d << std::endl;
std::cout << "m4\n" << m4 << std::endl; std::cout << "md\n" << m4 << std::endl;
}

Matthias

---


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/