Running portions of a loop in parallel with Octave


I have the following code that I need to run over a matrix with over 20000 rows. It takes several minutes to run and the datenum and str2double functions appear to be the bottlenecks. Since no calculation depends on previous ones is there a way to break the loop into multiple parts and have them execute in parallel? Any advice on optimising this code would be appreciated.

for i=1:length(DJI)
for j=3:7

Best Solution

Hmm. I'm more of a MATLAB person than Octave but maybe I can help (if you are still looking for a solution)

This looks like the I'm-reading-in-a-file-but-I-need-to-do-something-different-than-the-tool-provides problem (otherwise you could get away with dlmread which should be pretty fast).

If there were no alternative within Octave to be faster, I'd try using Java (for speed rather than threading); you can call Java from Octave. (though I haven't tried this in Octave, just the MATLAB equivalent)

The calls to str2double look awfully suspicious. You may be able to vectorize that, although a quick speed test on my part seems to confirm that this is a Slow Task, at least from within Octave:

octave-3.0.3.exe:15> s=sprintf('1 2\n3 4');
octave-3.0.3.exe:16> m=str2double(s)
m =

   1   2
   3   4

octave-3.0.3.exe:35> s=randn(5000,5);
octave-3.0.3.exe:36> z=num2str(s);
octave-3.0.3.exe:37> tic; s2=str2double(z); toc
Elapsed time is 18.9837 seconds.