【问题标题】:Trying to optimize some code and make it faster尝试优化一些代码并使其更快
【发布时间】:2012-08-09 12:59:04
【问题描述】:

我在 matlab 中有以下代码,非常慢。这段代码与堆栈溢出中的 previous post 有关我想知道是否有任何方法可以使 matlab 更快,当我运行代码时它应该显示图,有更新的图像,但它没有显示任何东西

%% Loading Data
cd('D:\MatlabTest\15-07SpeedSensitivity\0.3');
clear all
row=101;
column=311;

%%
%Coefficients Creation
N=5;
W = [0.005 0.10;0.10 0.20;0.20 0.30;0.30 0.40;0.40 0.50;0.50 0.60 ;0.60 0.70;0.70 0.80 ;0.80 0.90;0.90 1.0];
for ind=1:9
    wn = W(ind,:);
    [b,a] = butter(N,wn);
    bCoeff{ind}=b;
    aCoeff{ind}=a;
end
[bCoeff{10},aCoeff{10}]=butter(N,0.9,'high');

%%
%filter initialization
ZState = cell(1,10);
for i=1:10
    ZState{i} = zeros(max(length(aCoeff{i}), length(aCoeff{i})) - 1, 1); %# This is the initial filter state
end
%%
bands=10;
for b=1:bands
    Yout{b}{row, column}=[];
end

%%
j=1;
K = 1000:4000;
window = zeros(1,10);
figure;
y = 0;         %# Preallocate memory for output
j=0;
buffSize=10;
tempMean{row,column}=[];
Gibbs=(length(K)*3)/100;
fImg{1}(row,column)=0;
%load one image
for i = 1000:length(K)
    disp(i)
    str = int2str(i);
    str1 = strcat(str,'.mat');
    load(str1);
    D(:,:) = A(100:200 ,200:510);
    %go throught the columns and rows
    for p = 1:row
        for q = 1:column
            %calculte the temporal mean value based on previous ones
            if(size(tempMean{p,q})<buffSize) %init the first 10
                tempMean{p,q}=[D(p,q) tempMean{p,q}];
            else
                tempMean{p,q}=[D(p,q) tempMean{p,q}(1:end-1)];
            end
            if(mean2(tempMean{p,q})==0)
                x=0;
            else
                x = double(D(p,q)/mean2(tempMean{p,q}));
            end
            %filtering for 10 bands, based on the previous state
            for f = 1:10
                [y, ZState{f}] = filter(bCoeff{f},aCoeff{f},x,ZState{f});
                if(j<Gibbs)
                    continue;
                end
                if(size(Yout{f}{p,q})<10)%init the first 10 after Gibbs phenomenon
                    Yout{f}{p,q} = [y.^2 Yout{f}{p,q}];
                else
                    Yout{f}{p,q} = [y.^2 Yout{f}{p,q}(1:end-1)];
                    fImg{f}(p,q)=mean2(Yout{f}{p,q});
                end
            end
        end
    end
     if(size(fImg{1}(1,1))>1)   
    for k = 1:10
        subplot(5,2,1);
        subimage(fImg{k}*5000, [0 0.5]);
        colormap jet
    end
    pause(0.01);
     end
    j=j+1;
end
disp('Done Loading...')`

【问题讨论】:

  • 哇,这么多代码! SO 上的人们通常不倾向于处理需要理解几十行代码的问题,尽管也有例外。如果您发布几行您认为有问题的代码,而不是发布大量代码并要求人们找到模糊的错误/问题,您将获得更好的响应。发布短代码段的另一个好处是它允许其他用户执行本地测试。我无法获取您发布的内容并在本地运行,这让我更难为您提供帮助。
  • 在 Matlab 中查找瓶颈的一个很好的工具是使用 Profiler。它会生成一个很好的报告,告诉哪些代码行需要最多时间。我建议你试试看。

标签: matlab


【解决方案1】:

好吧,我没有1000.mat1001.mat、...、4000.mat,所以我无法真正正确地测试这个。

不管怎样,我可以随便告诉你的是

  • 嵌套循环通常是个坏主意。将大部分精力用于防止您拥有的四重嵌套循环

  • 大多数循环体都包含对外部非内置函数(means2int2str、...)的引用。这意味着无法应用 JIT 编译,for-loop 以解释器速度运行。

  • YoutfImg 的内容在每次最内层迭代时都会改变大小。应该避免改变大小或至少保持非常小的循环。

调整变量大小意味着Matlab需要创建一个更大的临时变量,将原始数组的内容复制到临时变量,将新数据附加到其中,将临时变量分配给原始变量,然后收拾残局。正如您所理解的,所有这些毫无意义的复制都需要很多时间,而您必须多次进行(您确实这样做了),所以:努力预分配整个数组

除此之外,我们不能没有mat 文件。按照@slayton 的建议,学习使用 Matlab profiler。我强调的要点可能会跳出来(数据的加载也会跳出来,但是这无法改进(除非所有个数据文件都适合你的记忆?))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 2020-01-13
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多