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

elwc picture elwc · Dec 26, 2012 · Viewed 8.1k times · Source

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?

Answer

carlosdc picture carlosdc · Dec 26, 2012

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.