提高 Matlab 的运行时间?

发布于 2024-11-18 18:23:36 字数 1405 浏览 3 评论 0原文

我有一些代码需要很长时间才能运行(几个小时),我认为这是因为它在 if 语句中进行了大量比较。我希望它运行得更快,有人有任何有用的建议来改进运行时间吗?如果有人对是什么导致代码变慢有不同的想法,那么我可以尝试修复它,我将不胜感激。

xPI = zeros(1,1783);
argList2 = zeros(1,1783);
aspList2 = zeros(1,1783);
cysList2 = zeros(1,1783);
gluList2 = zeros(1,1783);
hisList2 = zeros(1,1783);
lysList2 = zeros(1,1783);
tyrList2 = zeros(1,1783);

minList= xlsread('20110627.xls','CM19:CM25');
maxList= xlsread('20110627.xls','CN19:CN25');
N = length(pIList);
for i = 1:N
    if (argList(i)>= minList(1) && argList(i) <= maxList(1)) ...
        && (aspList(i)>= minList(2) && aspList(i) <= maxList(2)) ...
        && (cysList(i)>= minList(3) && cysList(i) <= maxList(3)) ...
        && (gluList(i)>= minList(4) && gluList(i) <= maxList(4)) ...
        && (hisList(i)>= minList(5) && hisList(i) <= maxList(5)) ...
        && (lysList(i)>= minList(6) && lysList(i) <= maxList(6)) ...
        && (tyrList(i)>= minList(7) && tyrList(i) <= maxList(7))

        xPI(i) = pIList(i);
        argList2(i) = argList(i);
        aspList2(i) = aspList(i);
        cysList2(i) = cysList(i);
        gluList2(i) = gluList(i);
        hisList2(i) = hisList(i);
        lysList2(i) = lysList(i);
        tyrList2(i) = tyrList(i);
        disp('passed test');
    end
end

I have some code that is taking a long time to run(several hours) and I think it is because it is doing a lot of comparisons in the if statement. I would like it to run faster, does anyone have any helpful suggestions to improve the runtime? If anyone has a different idea of what is slowing the code down so I could try and fix that it would be appreciated.

xPI = zeros(1,1783);
argList2 = zeros(1,1783);
aspList2 = zeros(1,1783);
cysList2 = zeros(1,1783);
gluList2 = zeros(1,1783);
hisList2 = zeros(1,1783);
lysList2 = zeros(1,1783);
tyrList2 = zeros(1,1783);

minList= xlsread('20110627.xls','CM19:CM25');
maxList= xlsread('20110627.xls','CN19:CN25');
N = length(pIList);
for i = 1:N
    if (argList(i)>= minList(1) && argList(i) <= maxList(1)) ...
        && (aspList(i)>= minList(2) && aspList(i) <= maxList(2)) ...
        && (cysList(i)>= minList(3) && cysList(i) <= maxList(3)) ...
        && (gluList(i)>= minList(4) && gluList(i) <= maxList(4)) ...
        && (hisList(i)>= minList(5) && hisList(i) <= maxList(5)) ...
        && (lysList(i)>= minList(6) && lysList(i) <= maxList(6)) ...
        && (tyrList(i)>= minList(7) && tyrList(i) <= maxList(7))

        xPI(i) = pIList(i);
        argList2(i) = argList(i);
        aspList2(i) = aspList(i);
        cysList2(i) = cysList(i);
        gluList2(i) = gluList(i);
        hisList2(i) = hisList(i);
        lysList2(i) = lysList(i);
        tyrList2(i) = tyrList(i);
        disp('passed test');
    end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倥絔 2024-11-25 18:23:36

您可以尝试对代码进行矢量化;我已经制作了一些示例数据集并复制了您在下面执行的一些操作。

matA1 = floor(rand(10)*1000); 
matB1 = floor(rand(10)*1000);

matA2 = zeros(10); 
matB2 = zeros(10);

minList = [10, 20]; 
maxList = [100, 200];

indicesToCopy = ( matA1 >= minList(1) ) & ( matA1 <= maxList(1) ) & ( matB1 >= minList(2) ) & ( matB1 <= maxList(2) );

matA2(indicesToCopy) = matA1(indicesToCopy); 
matB2(indicesToCopy) = matB1(indicesToCopy);

不知道这是否更快,您必须尝试一下。

编辑:
这并不重要,因为您只进行了两次调用,但是 xlsread 速度非常慢。您可以使用该函数的这种变体语法来加快这些调用的速度。

num = xlsread(filename, sheet, 'range', 'basic')

问题是范围参数被忽略并读取整个工作表,因此您将不得不正确地对结果进行索引。

You can try vectorising the code; I have made up some sample data sets and duplicated some of the operations you're performing below.

matA1 = floor(rand(10)*1000); 
matB1 = floor(rand(10)*1000);

matA2 = zeros(10); 
matB2 = zeros(10);

minList = [10, 20]; 
maxList = [100, 200];

indicesToCopy = ( matA1 >= minList(1) ) & ( matA1 <= maxList(1) ) & ( matB1 >= minList(2) ) & ( matB1 <= maxList(2) );

matA2(indicesToCopy) = matA1(indicesToCopy); 
matB2(indicesToCopy) = matB1(indicesToCopy);

No idea whether this is any faster, you'll have to try it out.

EDIT:
This doesn't matter too much since you're only making two calls, but xlsread is horribly slow. You can speed up those calls by using this variant syntax of the function.

num = xlsread(filename, sheet, 'range', 'basic')

The catch is that the range argument is ignored and the entire sheet is read, so you'll have to mess with indexing the result correctly.

鯉魚旗 2024-11-25 18:23:36

使用分析器查看哪些行或函数使用的执行时间最多。

通过矢量化您的代码,您可能可以大幅提高执行速度。代码。这意味着使用一次性对整个向量进行操作的操作,而不是使用 for 循环来迭代它。像这样的东西:

% make a logical vector indicating what you want to include
ii = (argList >= minList(1) & argList  <= maxList(1)) & ...

% use it
argList2(ii) = arglist(ii); % copies over every element where the corresponding ii is 1
...

Use the profiler to see which lines or functions are using the most execution time.

You can probably get a huge increase in execution speed by vectorizing your code. This means using operations which operate on an entire vector at once, instead of using a for-loop to iterate through it. Something like:

% make a logical vector indicating what you want to include
ii = (argList >= minList(1) & argList  <= maxList(1)) & ...

% use it
argList2(ii) = arglist(ii); % copies over every element where the corresponding ii is 1
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文