Re: for loop faster than vectorized



ML tries best to look at code and optimise it for you, though you should
never rely on it, MATLAB does tweak simple code.

As dan mentioned, try something more complicated. It might throw off the internal MATLAB 'tweaker' and you would
then observe the actual difference b/w vectorizing and not. Say do a rand inside the loop:


for m = 1:ntimes
sum1 = 0;
for idx1 = 1:size_mat
sum1 = sum1 + aa1(idx1)*bb1(idx1)+rand;
end
end



On Wed, 12 Mar 2008 09:40:40 -0400, DanK <dkominsky@xxxxxxxxxxxxxxxxxx> wrote:

On Mar 12, 8:54 am, "Stefan " <stefanfunkeber...@xxxxxx> wrote:
Hello,

one always can read that vectorized code and internal
functions are faster than using for loops and scripts. so i
tried to keep that in mind when programming. now, i wondered
why a part of a program slowed down after "optimizing" it.
below, i generated a minimal example of similar code that
shows the problem. the element wise product of two vectors
shall be summed up. the first version calculates it by using
a foor loop the second one uses "sum" and ".*". when running
with the profiler the second version takes more than twice
the time of the first. Why?

when rising the size of the vectors and lowering the number
of calculations the vectorized version gets faster than the
loop as it would be expected. but why is the loop faster
when using small vectors and running the calculation more
often? can i improve the calculation time of the loop at all
or is there no faster version for small arrays?

i'm working with the student version of R2007a.

thanks for your help.

clear all
close all
clc

ntimes = 1000000; % number of calculations
size_mat = 20; % length of vectors

aa1 = rand(size_mat,1);
bb1 = rand(size_mat,1);

aa2 = rand(size_mat,1);
bb2 = rand(size_mat,1);

for m = 1:ntimes
sum1 = 0;
for idx1 = 1:size_mat
sum1 = sum1 + aa1(idx1)*bb1(idx1);
end
end

for n = 1:ntimes
sum2 = sum(aa2.*bb2);
end

I don't know for certain, but my guess is that you just ran into JIT.
Ever since TMW introduced it, the old adage about vectorize for speed
isn't necessarily true. A few things to note about timing
computations:
1. Run your timings in a function (not a script, and not from the
command line)
2. Never trust the first time through (actually now, don't trust the
first couple of times through) for accurate timing (ML is still
figuring out how to accelerate it.
3. Adding in one more variant on the calculation:
sum3 = (aa1.')*bb1; That roughly splits the difference between the
other two methods.
4. Memory can often play a big role in the vectorize or not question.
If you are dealing with big arrays the relative speed can be very
different from smaller arrays.

HTH,
Dan

.



Relevant Pages

  • Re: freshmen needs help
    ... Most of us cringe when we see this unnecessary for loop (and you ... MATLAB is a dynamic typed language, ... you first do this calculation: ... plot of a square function? ...
    (comp.soft-sys.matlab)
  • Re: To breake a loop
    ... I am looking for the run time solution when loop is running and user need interrupt it according some conditions that were not known in the moment when program was written. ... Oh, and BTW, I guess you are aware that Ctrl-C kills the current calculation performed by Matlab? ...
    (comp.soft-sys.matlab)
  • how to do the programatic simulation
    ... What I want to do is to do the simulation in a loop. ... In matlab: ... do the calculation with the outputs from the sim ... But the problem is that the simulink can only load results to the workspace after the simulation ends, what I received is the results from the fist loop. ...
    (comp.soft-sys.matlab)
  • plotting data from a For loop
    ... I have a For loop and it gives me the results from my ... I get 26 results and I want to plot them but plot only ... plots the result of the last calculation. ... I'm new to matlab and have no idea how to get it to work. ...
    (comp.soft-sys.matlab)
  • Re: C# coding guidelines: use "this." or not when referring to member fields/properties within the
    ... Alphabetic for strings isn't quite so ... One example of where people go wrong is when they want to optimise loop ... implementation so that each iteration takes 10% less time will only ...
    (microsoft.public.dotnet.languages.csharp)