Making script execution go faster
1 回表示 (過去 30 日間)
古いコメントを表示
I have 36 similar scripts. The output of the scripts is how much the electricity consumption increases if a BEV is introduced. The combinations of households are investigated.
Script 1: the increase for one household
Script 2: the Increase for 2 households (if 2 households are randomly combined)
.
.
Script 36: The increase for 36 households (if 36 households are randomly combined(.
My problem is that the scripts are running very slowly. The execution is basically stuck.
I have a lot of for loops in the scripts. The higher number of the scripts, the more for loops. I wonder if there is a way to make the script faster?
I paste Script 3 as an example here. It combines every vehicle (Vehicle1,2,3) with every combination of household (Hcombos).
First it assigns vehicles to households (if there is 3 or more inhabitants there can be 2 vehicles)
for j = 1:nBEV
.
.
then it finds all the doubkes and removing them (i.e. two households cannot have the same car).
for jj3=1:numel(VID3)
.
.
VID1={}; %ID=Vehicles ID number
VID2={};
VID3={};
Vehicle1={};
Vehicle2={};
Vehicle3={};
IDcell2={};
GUD={};
for i2=1:length(Hcombos(:,1))
%State combinations
C=Hcombos(i2,1);
C2=Hcombos(i2,2);
C3=Hcombos(i2,3);
INCREASE2={};
HH2=Household(:, C)+Household(:, C2)+Household(:,C3);
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
%3----------------------------
if HHPerson(C3)>=x
for k3=1:nBEV
%There is only one car if
if k3==j
Vehicle3{j,k3}=BEV(:, j);
VID3{j,k3}=ID(j);
else
Vehicle3{j,k3}=BEV(:,k3);
VID3{j,k3}=[ID(k3)];
end
end
end
if HHPerson(C3)<x
for k33=1:nBEV
%There is only one car if
if k33==j
Vehicle3{j,k33}=BEV(:, j);
VID3{j,k33}=ID(j);
else
Vehicle3{j,k33}=BEV(:,k33);
VID3{j,k33}=[ID(k33)];
end
end
end
end
for jj3=1:numel(VID3)
V3=cell2mat(Vehicle3(jj3));
ID3=cell2mat(VID3(jj3));
for jj=1:numel(VID1)
V1=cell2mat(Vehicle1(jj));
ID1=cell2mat(VID1(jj));
if numel(intersect(ID1,ID3))
continue
end
for jj2=1:numel(VID2)
V2=cell2mat(Vehicle2(jj2));
ID2=cell2mat(VID2(jj2));
%Removes all doubles
if numel(intersect(ID1,ID2))
continue
end
INCREASE2{jj,jj2}=max(HH2+V1+V3+V2)./max(HH2);
end
end
end
GUD{i2}=INCREASE2;
%HUSGUD{i2}=[C(i2) CC(i2)];
end
for lol = 1:numel(GUD)
idx = ~cellfun(@isempty,GUD{lol});
tmp = zeros(size(GUD{lol})); % or perhaps NAN.
tmp(idx) = [GUD{lol}{idx}];
GUD{lol} = tmp;
end
0 件のコメント
採用された回答
Jan
2021 年 4 月 11 日
Avoid scripts but use functions. This can increase the speed substantially, but most of all the clarity and maintanability of the code is improved massively.
for i2=1:length(Hcombos(:,1))
This creates a temporary vector only to count the number of elements. Faster:
for i2 = 1:size(Hcombos, 1)
These IF-blocks are a waste of time:
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
In both cases exactly the same is performed, so omit the IF command and run:
Vehicle2{j, k22} = BEV(:,k22);
VID2{j, k22} = ID(k22);
Do not use cell2mat for a scalar cell element, because the direct indexing with the curly braces is faster:
ID2=cell2mat(VID2(jj2));
% Nicer and faster:
ID2 = VID2{jj2};
The iterative growing of array wastes a lot of resources. Do not define the initial arrays as empty cells, but use the final size fir a pre-allocation.
Do your self and the readers the favor to apply a proper indentation: ctrl-a ctrl-i. The less confusing the indentation is, the easier is the code to read.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Downloads についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!