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) DJI2(i,1)=datenum(char(DJI(i,2)),'yyyy-mm-dd'); for j=3:7 DJI2(i,j-1)=str2double(char(DJI(i,j))); end end
Best Answer
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: