Info
この質問は閉じられています。 編集または回答するには再度開いてください。
Making a slow script faster
    2 ビュー (過去 30 日間)
  
       古いコメントを表示
    
Hi, I have multiple scripts in my masers thesis that executes way too slow. I am not good at MATLAB so I could really use some advice.
The scripts calculates how much the maximum power demand would increase if Battery EVs were introduced. Both the cells in BEV and Household are expressing the load every minute of one year.
In the script below I investigate how much the power demand would increase if the load from 2 households are combined. Hcombos is a matrix with all the possible combinations. One condition is that if a household has 3 or more inhabitants, there can be two BEVs in the household.
My problem is that the script takes such a long time to execute. I have tried to decrease the number of households and Vehicles, but it is still too slow.
I am running multiple other scripts with the same appearance to combine 3,4... 20 households. But with this pace I will never be able to even run them.
How can I make my script faster? 
VID1={};
VID2={};
Vehicle1={};
Vehicle2={};
ID=429x1 %ID numbers of the vehicles
BEV=429x1 cell (every cell is a 525600x1 double)
Household=525600x20 double
x=3;
GUD={};
for i2=1:length(Hcombos(:,1))
    %State combinations
    C=Hcombos(i2,1);
    C2=Hcombos(i2,2);
    INCREASE2={};
    HH2=Household(:, C)+Household(:, C2);
   for j = 1:nBEV
        %If the household has three or more inhabitants then there can be
        %two vehicles
        if HHPerson(C)>=x
        for k=1:nBEV
 %There is only one car if
                if k==j
                    Vehicle1{j,k}=BEV(:, j);
                    VID1{j,k}=ID(j);
                else
                    Vehicle1{j,k}=BEV(:,k)+BEV(:,j);
                    VID1{j,k}=[ID(j) ID(k)];
                end
        end
        end
    if HHPerson(C2)>=x
        for k2=1:nBEV
             %There is only one car if
                if k2==j
                    Vehicle2{j,k2}=BEV(:, j);
                    VID2{j,k2}=ID(j);
                else
                    Vehicle2{j,k2}=BEV(:,k2)+BEV(:,j); 
                    VID2{j,k2}=[ID(j) ID(k2)];
                end
        end
    end
          if HHPerson(C)<x
        for k1=1:nBEV
 %There is only one car if
                if k1==j
                    Vehicle1{j,k1}=BEV(:, j);
                    VID1{j,k1}=ID(j);
                else
                    Vehicle1{j,k1}=BEV(:,k1);
                    VID1{j,k1}=[ID(k1)];
                end
        end
          end
           if HHPerson(C2)<x
        for k22=1:nBEV
             %There is only one car if
                if k22==j
                    Vehicle2{j,k22}=BEV(:, j);
                    VID2{j,k22}=ID(j);
                else
                    Vehicle2{j,k22}=BEV(:,k22); 
                    VID2{j,k22}=[ID(k22)];
                end
        end
           end
   end
    for jj=1:numel(VID1)
        V11=cell2mat(Vehicle1(jj));
        ID1=cell2mat(VID1(jj));
        for jj2=1:numel(VID2)
           V22=cell2mat(Vehicle2(jj2));
           ID2=cell2mat(VID2(jj2));
%Removes all doubles (A household cannot have the same vehicle)  
if numel(intersect(ID1,ID2))
    continue
end
            INCREASE2{jj,jj2}=max(HH2+V11+V22)./max(HH2);
        end
   end
    GUD{i2}=INCREASE2;
end
1 件のコメント
  Rik
      
      
 2021 年 4 月 11 日
				Have you tried running the profiler to see where your code spends the most time?
Changes in algorithm will improve the speed much more than normal optimization. E.g. if you change a moving window computation to a convolution, you might substantially speed up your code.
Do your testing with small batches of data. That will allow you to quickly test changes.
You can find a lot of advice in the book by Yair Altman. There is probably also a lot of advice to be found on other websites.
回答 (0 件)
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

