help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbiew need help optimizing code and understanding Octave


From: Thomas Ilnseher
Subject: Re: Newbiew need help optimizing code and understanding Octave
Date: Tue, 01 Apr 2008 14:32:59 +0200

Am Montag, den 31.03.2008, 09:59 -0700 schrieb malhotrag:
> 
> The code below runs really slow (it's not that complex though), so I
> would
> like tips from all on how to make it fast. I know I need to vectorize
> this
> but I don't really understand the concept of vectorization and so need
> expert advice.

I'm not an expert myself, but here is a short explanation of
vectorization:

you got an inner loop:

for j = ymin:ymax
        y = j;
        Y = y / r;
        .....
end for;

You got a loop variable j, which will be ymin, then ymin+1, then ...
then ymax.
for every iteration of the code you compute a new value Y = j / r;

to vectorize that, one would use an vector that contains all the values
over which j would loop:
y=ymin:ymax; # in C this would be an array.
then you divide the vector:

Y=y/r; # this operation is  carried out for every element in the y
vector.

when you perform stuff like Y^2, and Y is now an Vector, you have to
write Y.^2 (Y^2 would be the result of a vector multiplication). The
point say ovtave that you don't intend a vector multiplication, but want
to square the vector (array) element by element.

same goes for *, and /. (a vector addition is what you want, so + is ok)

there are a few stupid things in your code:
x = i;
y = j;

this stuff slows down the whole thing, as octave is interpreting the
code. use x and y as loop variables. eliminate those lines.
H = h / r;
S = s / r;

h, s, and r are initialized at the beginning of your code, but you
calculate H and S every iteration of the loop. move these calculations
out of the loop(s).









-- 
Thomas Ilnseher <address@hidden>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]