4

I have a simple matrix multiplication code in python (numpy)

import numpy as np
import time
a = np.random.random((70000,3000));
b = np.random.random((3000,100));
t1=time.time()
c = np.dot(a,b);
t2=time.time()
print 'Time passed is %2.2f seconds' %(t2-t1

It needs about 16 seconds to complete the multiplication (c = np.dot(a,b);) on one core. However when I run the same multiplication on Matab, it needs about 1 second on (6 cores) to complete the multiplication.

So, Why Matlab is 2.6 times faster than numpy for matrix multiplication? (The performance per core is important for me)

UPDATE I have tried the same thing this time using Eigen. And its performance is slightly better than Matlab. Eigen uses the same Blas implementation as Numpy uses. So the Blas implementation and not be the source of the drawback in the performance.

To make sure that the installed numpy used BLAS, I np.show_config()

enter code here
blas_info:
   libraries = ['blas']
   library_dirs = ['/usr/lib64']
   language = f77
lapack_info:
   libraries = ['lapack']
   library_dirs = ['/usr/lib64']
   language = f77

atlas_threads_info:
   NOT AVAILABLE

blas_opt_info:
   libraries = ['blas']
   library_dirs = ['/usr/lib64']
   language = f77
   define_macros = [('NO_ATLAS_INFO', 1)]

atlas_blas_threads_info:
   NOT AVAILABLE

lapack_opt_info:
   libraries = ['lapack', 'blas']
   library_dirs = ['/usr/lib64']
   language = f77
   define_macros = [('NO_ATLAS_INFO', 1)]

atlas_info:
   NOT AVAILABLE

lapack_mkl_info:
   NOT AVAILABLE

blas_mkl_info:
   NOT AVAILABLE

atlas_blas_info:
   NOT AVAILABLE

mkl_info:
   NOT AVAILABLE
6
  • How did you install Numpy? From an Ubuntu package perhaps? Commented Apr 3, 2012 at 17:52
  • 2
    that blas is reference blas from netlib - the slowest blas around. install atlas or mkl instead. Commented Apr 3, 2012 at 17:54
  • Yes, I used sudo apt-get install python-numpy Commented Apr 3, 2012 at 17:56
  • 1
    @iampat: that's known to be a very suboptimal build of Numpy. Compile it yourself after installing better BLAS libraries, or use EPD. Commented Apr 3, 2012 at 18:01
  • 1
    @iampat - Are you sure that Eigen is linking to the same blas implementation? For example, on my machine, the repo's version of numpy, which links to netlib's blas, takes ~50 seconds. Using EPD, which links to MKL's blas implementation, the calculation takes 0.7 seconds. The only difference is the blas implementation. Commented Apr 3, 2012 at 21:57

1 Answer 1

6

Try out the Enthought Python Distribution. For one it is linked to the Intel Math Kernel Library, which is highly optimized and used by MatLab.

Edit: Update for 2017. The Anaconda distribution is really the way to go these days.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, for your answer. I can not change my python distributions. But, I can the blas implementation .
I have tried the same thing this time using Eigen. And its performance is slightly better than Matlab. Eigen uses the same Blas implementation as Numpy uses. So the Blas implementation and not be the source of the drawback in the performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.