Eigen Matrix vs Numpy Array Effect

I read in this question that eigen has very good performance. However, I tried to compare eigen multiplication MatrixXi multiplication vs numpy array . And numpy works better (~ 26 seconds versus ~ 29). Is there a more efficient way to do this eigen ?

Here is my code:

Numpy:

 import numpy as np import time n_a_rows = 4000 n_a_cols = 3000 n_b_rows = n_a_cols n_b_cols = 200 a = np.arange(n_a_rows * n_a_cols).reshape(n_a_rows, n_a_cols) b = np.arange(n_b_rows * n_b_cols).reshape(n_b_rows, n_b_cols) start = time.time() d = np.dot(a, b) end = time.time() print "time taken : {}".format(end - start) 

Result:

 time taken : 25.9291000366 

Eigen:

 #include <iostream> #include <Eigen/Dense> using namespace Eigen; int main() { int n_a_rows = 4000; int n_a_cols = 3000; int n_b_rows = n_a_cols; int n_b_cols = 200; MatrixXi a(n_a_rows, n_a_cols); for (int i = 0; i < n_a_rows; ++ i) for (int j = 0; j < n_a_cols; ++ j) a (i, j) = n_a_cols * i + j; MatrixXi b (n_b_rows, n_b_cols); for (int i = 0; i < n_b_rows; ++ i) for (int j = 0; j < n_b_cols; ++ j) b (i, j) = n_b_cols * i + j; MatrixXi d (n_a_rows, n_b_cols); clock_t begin = clock(); d = a * b; clock_t end = clock(); double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; std::cout << "Time taken : " << elapsed_secs << std::endl; } 

Result:

 Time taken : 29.05 

I am using numpy 1.8.1 and eigen 3.2.0-4 .

+6
source share
2 answers

Edit:

 a = np.arange(n_a_rows * n_a_cols).reshape(n_a_rows, n_a_cols) b = np.arange(n_b_rows * n_b_cols).reshape(n_b_rows, n_b_cols) 

in

 a = np.arange(n_a_rows * n_a_cols).reshape(n_a_rows, n_a_cols)*1.0 b = np.arange(n_b_rows * n_b_cols).reshape(n_b_rows, n_b_cols)*1.0 

This gives a factor increase of 100 at least on my laptop:

 time taken : 11.1231250763 

vs

 time taken : 0.124922037125 

Unless you really want to multiply integers. Eigen also accelerates the multiplication of numbers with double precision (the number is replaced by MatrixXi with MatrixXd three times), but there I see only a factor of 1.5: time taken: 0.555005 versus 0.846788.

+3
source

My question was answered by @Jitse Niesen and @ggael in the comments.

I need to add a flag to enable optimization during compilation: -O2 -DNDEBUG (O is capital o, not zero).

After enabling this flag eigen code runs 0.6 seconds, and not ~29 seconds without it.

+2
source

Source: https://habr.com/ru/post/971742/


All Articles