Why isn't vectorization faster than repeating loop comparing 6 about years ago.

2 views (last 30 days)
About 6 years ago I tested VECTORIZATION as MATLAB recommended and found Vectorization speed up even to one tenth of the calucation time. (I wrote a book using MATLAB)
Now I test the same MATLAB functions but the times are almost same.
And MATLAB help says Vectorization is useful for simplication and so on. They don't empahsis 'Saving Time' that was said long time ago.
I assume only that Vector calcuation for multi threading or prcessing, But now MATLAB supports also multi processing for repeating loop.
Dyuman Joshi
Dyuman Joshi on 2 Dec 2023
Edited: Dyuman Joshi on 2 Dec 2023
Maybe the code is not optimitized to use vectorization properly/fully.
Maybe the functionality used for timings don't paint the whole picture. Example - "Sometimes programs run too fast for tic and toc to provide useful data. If your code is faster than 1/10 second, consider measuring it running in a loop, and then average to find the time for a single run."
It could be multiple various reasons. I could go on but I hope you get the idea.
We can't tell with certainity unless we see your code and the method used to check the code perfomance. So, please provide your code and mention which method you employed to test the timings.
And it's not like Vectorization for everything is faster than corresponding for loop.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 2 Dec 2023
Edited: John D'Errico on 2 Dec 2023
Things change. Vectorization OFTEN is faster. But nothing says it MUST ALWAYS be faster. Remember that vectorization often trades off memory for an explicit loop. But essentially always, vectorization will use internal, implicit loops. So if the external, explicit loops you wrote are now efficiently parsed, then vectorization need not be a big gain.
They have spent a great deal of effort making the parser generate more efficient code. (Why would they not do so?)
And of course, we don't see the code you wrote, so we don't know if it is truly efficient, or if there may be better ways to write it in a vectorized form. There are many ways to vectorize code.
For example, a common way to vectorize some problems might be to generate a complete set of all possible combinations from a set of nested loops, then you discard those you have no need for. But this may create a large memory burden, or you generated more combinations than you would have using carefully written loops.
What do I mean by that? A very simple case might be:
[u,v] = meshgrid(1:1000);
ind = u <= v;
uvsum = u(ind) + v(ind);
So a simple way to find the sum of all numbers u+v, where u<=v, and u,v come from the set 1:1000. But I could have written nested loops...
n = 1000;
uvsum = zeros(n*(n+1)/2,1);
k = 0;
for u = 1:1000
for v = u:1000
k = k + 1;
uvsum(k) = u + v;
The nested loops were constructed to generate only the elements we want to use, whereas the meshgrid solution generates more combinations from a set, then discards roughly 50% of them immediately. If the nested explicit loops are efficient, then you won't gain much.
Another common example of vectorization I see might be how to generate all positive integers less than 1e7, subject to the constraint that the sum of the digits is exactly 10. Then the student would be asked to determine how many of those numbers are divisible by 7. (I've just made up a simple example here.)
How might one do that in MATLAB? Surely I can think of at least 3 ways to do so. Some would involve nested loops. Some would involve generating the entire set of integers below 1e7, then discard those that fail the criteria, and count how many remain.
My point is, this problem would have very different solutions, depending on the approach one takes. Some would be seemingly efficient but require much memory, and discard very many elements.


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!