5

Using vectorization to replace for-loops may increase Matlab programs' speed significantly. Is it because the vectorized codes are runned in parallel?

Is vectorization also beneficial for program using NumPy or uBLAS?

zhanwu
  • 1,508
  • 5
  • 16
  • 27

2 Answers2

8

"Vectorized" code is usually faster in interpreted environments like Matlab and numpy because the vectorized versions often (but not always) run pre-compiled and optimized code written in C or FORTRAN. Parallel execution may, or may not, play a role in this.

Use vectorization in numpy usually results in performance improvement for this reason - often the routines are compiled C or FORTRAN which run much faster than native python code which must be run on the interpreter. Also numpy, being written largely in C, can sidestep the python global interpreter lock, which can greatly improve responsiveness in python code which uses threads.

talonmies
  • 70,661
  • 34
  • 192
  • 269
  • How about program using uBLAS, any difference between vectorized and normal version? Or, according to what you say, uBLAS (C++) is already fast without vectorization? – zhanwu May 16 '11 at 09:30
  • Sorry, I have no experience with uBLAS. How it behaves should be completely different to Matlab or numpy. In those, the big speed up from vectorization is mostly bypassing the interpreter. uBLAS is a template library which is always compiled into executable code from the C++ it contains. – talonmies May 16 '11 at 09:37
  • 4
    For uBLAS, I think they are referring to a different meaning of the word "vectorization" than what is meant by vectorizing code for MATLAB or numpy. For uBLAS, the vectorized version uses so-called vector instructions, CPU instructions that operate on multiple data at once. On Intel processors, these are the SSE instructions. – Robert Kern May 16 '11 at 13:35
  • It could well be. It is a pretty odd question to try and conflate the performance of matlab or numpy with that of a compiled library in the first place. – talonmies May 16 '11 at 13:43
2

I think that part of what makes vectorization faster is that it reduces the overhead associated with multiple function calls. Passing a vector to a function corresponds to a single call, whereas individually passing each element of that vector to the function corresponds to multiple calls.

user755443
  • 21
  • 1