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.

TestTime Ratio (Vec/Non-Vec)
Polynomial0.54731
Logarithm0.83906
Exponent0.92894
Multiplication0.95035
Vector Creation1.0592
Conditional1.5777
Continue reading

Bookmark and Share