help-octave
[Top][All Lists]
Advanced

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

Re: How to handle slow array loops?


From: James Sherman Jr.
Subject: Re: How to handle slow array loops?
Date: Wed, 1 Jun 2011 22:56:40 -0400

On Wed, Jun 1, 2011 at 9:01 PM, <address@hidden> wrote:
Hi,

please consider this simplified example:

---------------------------------------
function h = loop(range)

h = zeros(1,1000 + 1);
xx = rand(2, range);

for i=1:range
 for j=1:2
   t = round(xx(j, i) * 1000 + 1);
   h(t) = h(t) + 1;
 end
end

endfunction
---------------------------------------

'tic; h = loop(1000000); toc'

on 2.4GHz Pentium 4 with 1GB RAM takes more then 340 seconds

Every added zero in that range adds another zero to test time

As this loop in Fortran [1] performs ~3000 times faster I'm
wondering
what can I do to speed up this Octave code?

Also some explanation why this is so slow is much appreciated

As to why, the short answer is that your comparing compiled code to a script.  I'm not an expert on this, so someone more knowledgeable that me can explain in more detail.

As to how to speed up this code, you could do something like:
function h = loop(range)

xx = rand(2, range);

t = round(xx(:) * 1000 + 1);
h = histc(t, 1:1001);

endfunction

I think will do the same thing that your function does.

Hope this helps.

reply via email to

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