Re: for loop faster than vectorized
- From: "Ashish Uthama" <ashishu@xxxxxxxxxx>
- Date: Wed, 12 Mar 2008 13:26:27 -0400
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
.
- References:
- for loop faster than vectorized
- From: Stefan
- Re: for loop faster than vectorized
- From: DanK
- for loop faster than vectorized
- Prev by Date: Re: ??? Error using ==> *
- Next by Date: LMI problem
- Previous by thread: Re: for loop faster than vectorized
- Next by thread: Re: MATLAB SOURCE CODE FOR JPEG2000
- Index(es):
Relevant Pages
|