help-octave
[Top][All Lists]
Advanced

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

Re: Octave too slow runnig some instruction


From: Moo
Subject: Re: Octave too slow runnig some instruction
Date: Thu, 24 Jun 2010 23:46:16 +0200

In general, when you have code that's running to slow and you don't know why, one way to figure out what is happening is to time all the significant portions of your code to see how long it takes.  For example, I modified your function, putting tics and tocs around the parts of the code I thought may be the culprits:

function [h,times] = make_random_direction(n,N,sigma)

times = zeros(3,1);

x=zeros(N,1);
y=zeros(N,1);

tic
for i=1:N
    x(i) = normrnd(0,sigma(i)*sigma(i),1,1);
    y(i) = normrnd(0,sigma(i)*sigma(i),1,1);
end
times(1) = toc;

h = zeros(n,2);

tic
t = linspace(0,2*pi,n);
times(2) = toc;

tic
for i=1:n
    for j=1:N
        h(i,1) = h(i,1) + x(j)*cos(t(i)*j);
        h(i,2) = h(i,2) + y(j)*sin(t(i)*j);
    end
end
times(3) = toc;

end

If you use the same inputs, and look at the "times" vector after running a sample code, you'll notice that it's the last part (the double for-loop) that's taking the large majority of the time.  Jaroslav's suggestions speed things up quite a bit, and he's right; whenever you can, try to avoid using for loops and get in the habit of trying to operate on vectors/matrices instead.


On Thu, Jun 24, 2010 at 11:38 AM, sd83 <address@hidden> wrote:

Dear all,

I'm working on a program and I have wrote a quite simple m-file which
generates a random closed curve. I have runned it by octave versions 3.0.5,
3.2, 3.2.4 but always the same problem: too slow.
The executing time of Octave is 35 seconds, matlab's executing time is less
than 1 second!
Why?
The problem is that I will have running this functions thousands of times
into my program!

Can anyone help me?

Thank you very much.

--
View this message in context: http://octave.1599824.n4.nabble.com/Octave-too-slow-runnig-some-instruction-tp2266693p2266693.html
Sent from the Octave - General mailing list archive at Nabble.com.
_______________________________________________
Help-octave mailing list
address@hidden
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave


reply via email to

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