4

I'm considering using Python for my future codes but I'm surprised by the differences in computation time.

For my example, I calculate the decomposition into singular values of a 1000x1000 random matrix, attached the two codes :

Matlab (0.38s):

M = randn(1000,1000);
[U,S,V] = svd(M);

Python (2.7s):

import numpy as np
M = np.random.randn(1000,1000)
U,S,V = np.linalg.svd(M)

Does the comparison between these two codes seem fair? I thought this type of functions were optimized in fortran/C langage for python, how is it possible that matlab is that faster in such conditions?

Thomas
  • 141
  • 3
  • 2
    A simple reason is that MATLAB is industry software and they can spend a lot of money on optimising things. Another reason may be that MATLAB can leverage multiple CPUs but I'm not sure about this. – Denziloe Sep 12 '18 at 20:19
  • Thanks for your comment, so Python does not support multithreading natively ? – Thomas Sep 12 '18 at 20:21
  • 1
    @Denziloe Are your guesses based on any facts whatsoever? – zvone Sep 12 '18 at 20:21
  • 1
    @zvone Only that I know MATLAB builtins use multithreading whereas Python tends not to. – Denziloe Sep 12 '18 at 20:23
  • @Thomas By the way, are you only timing the svd function, or the whole thing? – Denziloe Sep 12 '18 at 20:23
  • @Denziloe the whole thing for both codes – Thomas Sep 12 '18 at 20:24
  • In Octave the MATLAB code is noticeably longer than the numpy code. – hpaulj Sep 12 '18 at 20:24
  • @Thomas In which case you should isolate the timing of `svd` if that's what you're interested in. `randn` may have different functionality / implementations. – Denziloe Sep 12 '18 at 20:25
  • `svd` is basically a couple of matrix multiplications, and the proposed dupe tells you why MATLAB's so fast in it. – Adriaan Sep 12 '18 at 20:25
  • Source for MATLAB svd multithreaded: https://uk.mathworks.com/discovery/matlab-multicore.html – Denziloe Sep 12 '18 at 20:26
  • @Denziloe only timing the svd for both codes lead to really close computation times. I'll check these links, there might be a lot of relevant information in them – Thomas Sep 12 '18 at 20:27
  • There is a great answer made by @reverse_engineer in the similar question pointed out by Adriaan, my question is just a particular case of a more general one about matrix multiplication, I'll accept this post as being a duplicate – Thomas Sep 12 '18 at 20:39
  • I'm voting to keep this open. The similar question only explains why matlab is faster than naively implemented c++ code, numpy should be taking advantage of similar optimizations. – user2699 Sep 12 '18 at 20:52
  • More relevant questions are https://stackoverflow.com/questions/18516605/difference-on-performance-between-numpy-and-matlab or https://stackoverflow.com/questions/41793670/why-matlab-matrix-inversion-is-faster-than-numpy?rq=1 – user2699 Sep 12 '18 at 21:00
  • 1
    Finally solved this issue thanks to a coworker, it turns out that my numpy library was outdated. A simple pip install numpy --upgrade and I was getting comparable computing times to matlab, about 0.6s – Thomas Sep 13 '18 at 15:46

0 Answers0