Vectorization in Matlab Not Always Necessary

What separates Matlab from many other programming languages is the ability to vectorize code.  Vectorization allows a programmer to write code that is more intuitive, more concise, and often faster than using standard for, while and if statements.  If you have to do a large data processing task or need to create real-time application that does a large number of mathematical operations, vectorization is often a good option.  However, vectorization is not always the faster alternative for time-sensitive tasks.   I created tests for six tasks and compared the amount of time needed to run the vectorized and non-vectorized code.  The results were mixed, showing a decrease in running time for vectorized code on some tasks and a increase in running time on other tasks.  Tests involving mathematical operations ran faster using vectorized code, while tests involving conditional operators and vector creation ran faster for non-vectorized code.

[table id=13 /] Continue reading