Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does vectorized code run faster than for loops in MATLAB?

Tags:

I've read this but I still don't understand why vectorized code is faster.

In for loops, I can use parfor to for parallel computation. If vectorized code is faster, does it means that it is automatically parallelized?

like image 842
elwc Avatar asked Dec 26 '12 02:12

elwc


People also ask

Why is Vectorised code faster?

Vectorization is a type of parallel processing. It enables more computer hardware to be devoted to performing the computation, so the computation is done faster.

Is vectorized code faster?

No, vectorized code is not always faster. If the vectorization needs the creation of large temporary arrays, loops are often faster.

How vectorization is helpful in MATLAB?

Vectorization is one of the core concepts of MATLAB. With one command it lets you process all elements of an array, avoiding loops and making your code more readable and efficient. For data stored in numerical arrays, most MATLAB functions are inherently vectorized.


1 Answers

No. You're mixing two important concepts:

  • MATLAB is designed to perform vector operations really quickly. MATLAB is an interpreted language, which is why loops are so slow in it. MATLAB sidesteps this issue by providing extremely fast (usually written in C, and optimized for the specific architecture) and well tested functions to operate on vectors. There is really no magic here, it is just a bunch of hard work and many years of constant small improvements.

Consider for example a trivial case such as the following:

s=0; for i=1:length(v),     s = s+v(i); end 

and

sum(v) 

you should probably use tic and toc to time these two functions to convince yourself of the difference in runtime. There are about 10 similar commonly used functions that operate on vectors, examples are: bsxfun, repmat, length, find. Vectorization is a standard part of using MATLAB effectively. Until you can vectorize code effectively you're just a tourist in the world of MATLAB not a citizen.

  • Recent versions of MATLAB provide parfor. parfor is not a silver bullet, it is a tool that can be used and misused (try parfor on the sum example above). Not all fors can be parfored. parfor is designed for task-parallel types of problems where each iteration of the loop is independent of each other iteration. This is a key requirement for using a parfor-loop.

While in many cases parfor can help a lot the type of loops that can be parfored for very large gains occur seldomly.

like image 121
carlosdc Avatar answered Sep 25 '22 07:09

carlosdc