与八度并行运行循环的一部分? [英] Running portions of a loop in parallel with Octave?

查看:71
本文介绍了与八度并行运行循环的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码需要在具有20000行以上的矩阵上运行.它需要几分钟才能运行,并且datenum和str2double函数似乎是瓶颈.由于没有计算依赖于先前的计算,是否有办法将循环分成多个部分并让它们并行执行?关于优化此代码的任何建议将不胜感激.

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

推荐答案

嗯.我比Octave更像是MATLAB的人,但也许我可以帮忙(如果您仍在寻找解决方案)

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

这看起来像是我正在阅读一个文件,但是我需要做一些不同于工具的问题(否则您可以摆脱dlmread的困扰)应该很快).

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).

如果Octave中没有其他选择可以使速度更快,我会尝试使用Java(为了提高速度而不是线程化);您可以从Octave调用Java . (尽管我没有在Octave中尝试过,只是在MATLAB中等效)

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)

对str2double的调用看起来非常可疑.您也许可以将其向量化,尽管我的快速速度测试似乎确认这是一个缓慢的任务,至少在八度内如此:

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.

这篇关于与八度并行运行循环的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆