1

This is related to several questions that discuss the speed of numpy vs Matlab. However most of them have several matrix operations than a single operation. E.g. Difference on performance between numpy and matlab

For me, the time numpy take just to invert a random matrix is approximately 5 times slower than that of matlab.

Here is the matlab script,

N = 1000;

B = randn(N,N);
h = tic;
T = 40;
for i=1:40

    Rinv = (B)^(-1);
end
toc(h)/40

This gives an average values of 0.08 seconds approximately.

While this python script gives 0.4 seconds (approx).

import numpy as np 
from numpy import linalg as LA
import time 

N=1000
R = np.random.random((N,N))
T=40

t1 = time.clock()
for i in range(0,T):
    Rinv = LA.inv(R)
t2 = time.clock()
print 'avg time for inverse ',(t2-t1)/T

Is there any reason for this, or anyway to improve python performance ? I have already implemented my work on Python and I am worried whether I will have to port all my code to matlab. I am working on Ubuntu 16.04, Python 2.7, Matlab R2016b.

I have read that the time is not a good module for execution time comparisons, I feel this is something more than that.

5
  • 2
    I myself would also choose 40 random matrices rather than a single one. Commented Jan 22, 2017 at 17:02
  • 4
    You should use timeit to get a reliable benchmark in MATLAB. Taking the inverse of the same variable and storing it in the same variable within a loop is hardly going to be a good estimate of execution time especially considering JIT acceleration Commented Jan 22, 2017 at 17:07
  • 1
    This is a rather poor benchmark - in both cases you should be removing the loop from the timing, and using a more reliable timing mechanism. Commented Jan 22, 2017 at 17:12
  • @JamesKPolk I thought since generating random numbers is different for two methods. It may not be fair to compare just the inversion time. Will check that anyway. Commented Jan 22, 2017 at 17:20
  • @Suever Thanks. I am trying to get the results with timeit module something that I haven't used. Commented Jan 22, 2017 at 17:37

1 Answer 1

4

On my computer (Windows, python 3.5, numpy 1.11.2) :

In [6]: %timeit inv(a)
10 loops, best of 3: 86 ms per loop

edit:

or, without Ipython:

>>>timeit.timeit('inv(a)','from __main__ import inv,a',number=100)/100

which is similar to Matlab.

to know what code is used in the background, check it :

In [12]: np.__config__.show() 
blas_mkl_info:
include_dirs = ['c:/users/bruno/miniconda3\\Library\\include']
libraries = ['mkl_core_dll', 'mkl_intel_lp64_dll', 'mkl_intel_thread_dll']
...
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I am trying to run it using timeit, can you please post the code ? I wasn't able get it work using timeit module.
B.M. used the IPython or Jupyter console that provides a so-called "magic" feature for timing.
t= timeit.Timer("B=np.linalg.inv(R)","import numpy as np;R=np.random.random((1000,1000))");t.timeit(number=100) This gave me 9.95. Which is close to 90 ms on average. So I think using time module was the problem. Never though it has such an importance. Thank you !
I edit my post to manual timeit . I encourage you ta adopt Ipython ;)

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.