I use the following code to test Eigen perfomance.
#include <iostream>
#include <chrono>
#define EIGEN_NO_DEBUG
#include <eigen3/Eigen/Dense>
#include <cblas.h>
using namespace std;
using namespace std::chrono;
int main()
{
int n = 3000;
high_resolution_clock::time_point t1, t2;
Eigen::MatrixXd A(n, n), B(n, n), C(n, n);
t1 = high_resolution_clock::now();
C = A * B;
t2 = high_resolution_clock::now();
auto dur = duration_cast<milliseconds>(t2 - t1);
cout << "eigen: " << dur.count() << endl;
t1 = high_resolution_clock::now();
cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans,
n, n, n, 1.0, A.data(), n, B.data(), n, 1.0, C.data(), n);
t2 = high_resolution_clock::now();
dur = duration_cast<milliseconds>(t2 - t1);
cout << "cblas: " << dur.count() << endl;
return 0;
}
I compile it with the following command:
g++ test.cpp -O3 -fopenmp -lblas -std=c++11 -o test
The results are:
eigen: 1422 ms
cblas: 432 ms
Am i doing something wrong? According to their benchmark it should be faster.
Another problem is that using numpy i get 24 ms
import time
import numpy as np
a = np.random.random((3000, 3000))
b = np.random.random((3000, 3000))
start = time.time()
c = a * b
print("time: ", time.time() - start)
*
is element-wise multiplication. Changec = a * b
toc = a.dot(b)
. Or, if you are using a sufficiently new version of Python 3 and numpy, you can writec = a @ b
. – Warren Weckesser