Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is vectorization beneficial for Matlab programs? Is it the same for NumPy and Boost(uBLAS)?

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?

like image 453
zhanwu Avatar asked May 16 '11 09:05

zhanwu


2 Answers

"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.

like image 71
talonmies Avatar answered Sep 28 '22 04:09

talonmies


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.

like image 43
user755443 Avatar answered Sep 28 '22 05:09

user755443