# How to reduce the Run time and improve the performance of my code

조회 수: 1 (최근 30일)
Kumar 2020년 11월 20일
답변: Prudhvi Peddagoni 2020년 11월 24일
Hello,
I would like to improve the performance of code and reduce run time.
Explanation: Here I am loading multiple mat files (now I will load 8 files) and calculate the maximum deviation and plot the results wrt RotorSpeed.
%Program to find the Maximum Phase Current Deviation at certain speed
% range
% Load the measuremnet file
files = dir('*.mat'); % look for .mat files in the folder
for k = 1:numel(files)
% Assign the data from the files to variables
RotSpeed = abs(S.M1.Y(34).Data); % RotorSpeed
DevU = (S.M1.Y(20).Data); % Deviation of U
DevV = (S.M1.Y(21).Data); % Deviation of V
DevW = (S.M1.Y(22).Data); % Deviation of W
DevX = (S.M1.Y(23).Data); % Deviation of X
DevY = (S.M1.Y(24).Data); % Deviation of Y
% Variables for maximum deviation of phase currents U, V, W, X, Y
maxDevU =zeros; maxDevV = zeros; maxDevW = zeros; maxDevX = zeros; maxDevY = zeros;
% Variable for the maximum deviation of phase currents
for i= 1:length (RotSpeed)-1 % Calculate the MaxDeviation of U, V, W, X, Y
maxDevU(i) = max(DevU(i:i));
maxDevV(i) = max(DevV(i:i));
maxDevW(i) = max(DevW(i:i));
maxDevX(i) = max(DevX(i:i));
maxDevY(i) = max(DevY(i:i));
maxDev = zeros;
maxDev(i)= max([maxDevU(i),maxDevV(i),maxDevW(i),maxDevX(i),maxDevY(i)]);
end
for j= 1:length(RotSpeed)-1 % Calculate the Maximum Deviation of UVWXY at certain speeds
if RotSpeed(j) > 0.1 && RotSpeed(j)<= 1
maxDev0(j) = max(maxDev(j));
maxDev0_0 = max(maxDev0);
% maxDevTh0(j) = max(maxDev0_0);
elseif RotSpeed(j) >1 && RotSpeed(j+1) <= 500
maxDev1(j) = max(maxDev(j));
maxDev1_1 = max(maxDev1);
% maxDevTh1 = max(maxDev1_1);
elseif RotSpeed(j)>500 && RotSpeed(j+1)<=1000
maxDev2(j)= max(maxDev(j));
maxDev2_2 = max(maxDev2);
% maxDevTh2 = max(maxDev2_2);
elseif RotSpeed(j)>1000 && RotSpeed(j+1) <=1500
maxDev3(j)= max(maxDev(j));
maxDev3_3 = max(maxDev3);
% maxDevTh3 = max(maxDev3_3);
elseif RotSpeed(j)>1500 && RotSpeed(j)<=2000
maxDev4(j)= max(maxDev(j));
maxDev4_4 = max(maxDev4);
% maxDevTh4 = max(maxDev4_4);
elseif RotSpeed(j)>2000 && RotSpeed(j)<=2500
maxDev5(j)= max(maxDev(j));
maxDev5_5 = max(maxDev5);
% maxDevTh5 = max(maxDev5_5);
elseif RotSpeed(j)>2500 && RotSpeed(j)<=3000
maxDev6(j)= max(maxDev(j));
maxDev6_6 = max(maxDev6);
% maxDevTh6 = max(maxDev6_6);
elseif RotSpeed(j)>3000 && RotSpeed(j)<=3500
maxDev7(j)= max(maxDev(j));
maxDev7_7 = max(maxDev7);
% maxDevTh7 = max(maxDev7_7);
elseif RotSpeed(j)>3500 && RotSpeed(j)<=4000
maxDev8(j)= max(maxDev(j));
maxDev8_8 = max(maxDev8);
% maxDevTh8 = max(maxDev8_8);
elseif RotSpeed(j)>4000 && RotSpeed(j)<=4500
maxDev9(j)= max(maxDev(j));
maxDev9_9 = max(maxDev9);
% maxDevTh9 = max(maxDev9_9);
elseif RotSpeed(j)>4500 && RotSpeed(j)<=5000
maxDev10(j)= max(maxDev(j));
maxDev10_10 = max(maxDev10);
% maxDevTh10 = max(maxDev10_10);
elseif RotSpeed(j)>5000 && RotSpeed(j)<=5500
maxDev11(j)= max(maxDev(j));
maxDev11_11 = max(maxDev11);
% maxDevTh11 = max(maxDev11_11);
elseif RotSpeed(j)>5500 && RotSpeed(j)<=6000
maxDev12(j)= max(maxDev(j));
maxDev12_12 = max(maxDev12);
% maxDevTh12 = max(maxDev12_12);
elseif RotSpeed(j)>6000 && RotSpeed(j)<=6500
maxDev13(j)= max(maxDev(j));
maxDev13_13 = max(maxDev13);
% maxDevTh13 = max(maxDev13_13);
elseif RotSpeed(j)>6500 && RotSpeed(j)<=7000
maxDev14(j)= max(maxDev(j));
maxDev14_14 = max(maxDev14);
% maxDevTh14 = max(maxDev14_14);
elseif RotSpeed(j)>7000 && RotSpeed(j)<=7500
maxDev15(j)= max(maxDev(j));
maxDev15_15 = max(maxDev15);
% maxDevTh15 = max(maxDev15_15);
elseif RotSpeed(j)>7500 && RotSpeed(j)<=8000
maxDev16(j)= max(maxDev(j));
maxDev16_16 = max(maxDev16);
% maxDevTh16 = max(maxDev16_16);
elseif RotSpeed(j)>8000 && RotSpeed(j)<=8500
maxDev17(j)= max(maxDev(j));
maxDev17_17 = max(maxDev17);
% maxDevTh17 = max(maxDev17_17);
elseif RotSpeed(j)>8500 && RotSpeed(j)<=9000
maxDev18(j)= max(maxDev(j));
maxDev18_18 = max(maxDev18);
% maxDevTh18 = max(maxDev18_18);
elseif RotSpeed(j)>9000 && RotSpeed(j)<=9500
maxDev19(j)= max(maxDev(j));
maxDev19_19 = max(maxDev19);
% maxDevTh19 = max(maxDev19_19);
elseif RotSpeed(j)>9500 && RotSpeed(j)<=10000
maxDev20(j)= max(maxDev(j));
maxDev20_20 = max(maxDev20);
% maxDevTh20 = max(maxDev20_20);
elseif RotSpeed(j)>10000 && RotSpeed(j)<=10500
maxDev21(j)= max(maxDev(j));
maxDev21_21 = max(maxDev21);
% maxDevTh21 = max(maxDev21_21);
elseif RotSpeed(j)>10500 && RotSpeed(j)<=11000
maxDev22(j)= max(maxDev(j));
maxDev22_22 = max(maxDev22);
% maxDevTh22 = max(maxDev22_22);
elseif RotSpeed(j)>11000 && RotSpeed(j)<=11500
maxDev23(j)= max(maxDev(j));
maxDev23_23 = max(maxDev23);
% maxDevTh23 = max(maxDev23_23);
elseif RotSpeed(j)>11500 && RotSpeed(j)<=12000
maxDev24(j)= max(maxDev(j));
maxDev24_24 = max(maxDev24);
% maxDevTh24 = max(maxDev24_24);
elseif RotSpeed(j)>12000 && RotSpeed(j)<=12500
maxDev25(j)= max(maxDev(j));
maxDev25_25 = max(maxDev25);
% maxDevTh25 = max(maxDev25_25);
elseif RotSpeed(j)>12500 && RotSpeed(j)<=13000
maxDev26(j)= max(maxDev(j));
maxDev26_26 = max(maxDev26);
% maxDevTh26 = max(maxDev26_26);
elseif RotSpeed(j)>13000 && RotSpeed(j)<=13500
maxDev27(j)= max(maxDev(j));
maxDev27_27 = max(maxDev27);
% maxDevTh27 = max(maxDev27_27);
elseif RotSpeed(j)>13500 && RotSpeed(j)<=14000
maxDev28(j)= max(maxDev(j));
maxDev28_28 = max(maxDev28);
% maxDevTh28 = max(maxDev28_28);
elseif RotSpeed(j)>14000 && RotSpeed(j)<=14500
maxDev29(j)= max(maxDev(j));
maxDev29_29 = max(maxDev29);
% maxDevTh29 = max(maxDev29_29);
elseif RotSpeed(j)>14500 && RotSpeed(j)<=15000
maxDev30(j)= max(maxDev(j));
maxDev30_30 = max(maxDev30);
% maxDevTh30 = max(maxDev30_30);
elseif RotSpeed(j)>15000 && RotSpeed(j)<=15500
maxDev31(j)= max(maxDev(j));
maxDev31_31 = max(maxDev31);
% maxDevTh31 = max(maxDev31_31);
elseif RotSpeed(j)>15500 && RotSpeed(j)<=16000
maxDev32(j)= max(maxDev(j));
maxDev32_32 = max(maxDev32);
% maxDevTh32 = max(maxDev32_32);
elseif RotSpeed(j)>16000 && RotSpeed(j)<=16500
maxDev33(j)= max(maxDev(j));
maxDev33_33 = max(maxDev33);
% maxDevTh33 = max(maxDev33_33);
elseif RotSpeed(j)>16500 && RotSpeed(j)<=17000
maxDev34(j)= max(maxDev(j));
maxDev34_34 = max(maxDev34);
% maxDevTh34 = max(maxDev34_34);
elseif RotSpeed(j)>17000 && RotSpeed(j)<=17500
maxDev35(j)= max(maxDev(j));
maxDev35_35 = max(maxDev35);
% maxDevTh35 = max(maxDev35_35);
elseif RotSpeed(j)>17500 && RotSpeed(j)<=18000
maxDev36(j)= max(maxDev(j));
maxDev36_36 = max(maxDev36);
% maxDevTh36 = max(maxDev36_36);
end
end
%ploting RotorSpeed steps versus Maximum deviation
n_Step = (0:500:18000); % x axis RotSpeed in steps of 500rpm
% y axis maximum deviation at each speed range of 500 rpm till 18000rpm
maxDevn_n = ([maxDev0_0, maxDev1_1, maxDev2_2, maxDev3_3, maxDev4_4, maxDev5_5, maxDev6_6...
maxDev7_7, maxDev8_8, maxDev9_9, maxDev10_10, maxDev11_11, maxDev12_12...
maxDev13_13, maxDev14_14, maxDev15_15, maxDev16_16, maxDev17_17, maxDev18_18...
maxDev19_19, maxDev20_20, maxDev21_21, maxDev22_22, maxDev23_23...
maxDev24_24, maxDev25_25, maxDev26_26, maxDev27_27, maxDev28_28...
maxDev29_29, maxDev30_30, maxDev31_31, maxDev32_32, maxDev33_33...
maxDev34_34, maxDev35_35, maxDev36_36]);
plot(n_Step,maxDevn_n);
hold on
xlabel('RotorSpeed rpm')
ylabel('PhaseCurrent A')
end

댓글을 달려면 로그인하십시오.

### 답변 (1개)

Prudhvi Peddagoni 2020년 11월 24일
Hi,
Instead of the last for-loop, you can do this:
maxDev0_0=max(maxDev(RotSpeed(j) > 0.1 && RotSpeed(j)<= 1)); %this is for the first if condition only
So basically you are computing maximum at every iteration when you finding the maximum at the end is enough.
Hope this helps.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by