MATLAB: Is vectorization preserve scaling of runtime

runtimevectorization

Suppose I have a calculation, in which some for cycles is required inside each other. For example: for n = 1:N; for j = 1:J; for m = 1:M*J; In this case runtime scales with N, and M linearly, but runtime grows quadratically with J. If we can vectorize the calculation somehow, vectorization can speed up. But is scaling remain the same?

Best Answer

Even with vectorization the runtime will depend on the number of calculations. Sometimes vectorized code requires large temporary arrays, which do not match into the processor cache or the RAM. Then the slower RAM or even slower hard disk is used, which can degrade the speed massively. But as long as the same kind of memory can be used, the order of the execution time is the same for loops and vectorized code. Roughly.
But sometimes, vectorized code is multi-threaded, e.g. in the sum command. Then the number of free cores matters in addition above a certain size of the input. While sum(2e5) needs about twice as long as sum(1e5), this does not hold true for sum(10e5) and sum(5e5).