0

i am somekind of "advanced" beginner in programing. I couldn't find answer to my question, i found this, MATLAB Speed Difference in Identical Code, but it is not exactly what i'm looking for. I have the following problem, my script or function consists of couple of segments, when i run them manually with Run Section, i need something like 15 sec. including scroling and clicking. When i start them as one function i need 72 sec. The code is:

a=diff(Min_ZR);
b=zeros(38000000,1)*NaN;
i=1;

while i<length(a)
if a(i)==1;
    b(i)=1;
    i=i+1;

else
    b(i)=0;
    i=i+1;
end
end


 b(i:end) = [];
%%
iL=2;
kL=2;
L1=zeros(38000000,1)*0;

while iL<length(test_L)

if test_L(iL)==1

L1(iL)=L1(kL-1)+1;
iL=iL+1;
kL=iL;
else
    L1(iL)=L1(iL-1);
    iL=iL+1;

end
end
L1(iL:end)=[];

%%

iR=2;
kR=2;
L2=zeros(38000000,1)*0;

while iR<length(test_R)

if test_R(iR)==1

L2(iR)=L2(kR-1)+1;
iR=iR+1;
kR=iR;
else
    L2(iR)=L2(iR-1);
    iR=iR+1;

end
end
L2(iR:end)=[];
%%

iZ=2;
kZ=2;
ZR=zeros(38000000,1)*0;

%while i<length(test_R)
while iZ<length(b)
if b(iZ)==1

ZR(iZ)=ZR(kZ-1)+1;
iZ=iZ+1;
kZ=iZ;
else
    ZR(iZ)=ZR(iZ-1);
    iZ=iZ+1;

end
end
ZR(iZ:end)=[];





%%
L1=L1/4;
L2=L2/4;
ZR=ZR/8*34/22;
i=1;
diff_L1=zeros(38000000,1)*NaN;
diff_L2=zeros(38000000,1)*NaN;
while i<=length(ZR)

diff_L1(i)=L1(i)-ZR(i);
diff_L2(i)=L2(i)-ZR(i);
i=i+1;
end
diff_L1(i:end)=[];
diff_L2(i:end)=[];

l=length(ZR);
Drehmoment=zeros(l,1);
Drehmoment(1:10000)=100;
Drehmoment(10001:end)=380;
%%

figure % new figure
[hAx,hLine1,hLine2] = plotyy(ZR,diff_L2,ZR,Drehmoment);

title('Zahnradwandern 30.03.2016')
xlabel('Gesamtumdrehungen /1 ')

ylabel(hAx(1),'Differenzumdrehungen /1 ') % left y-axis
ylabel(hAx(2),'Drehmoment / Nm') % right y-axis

set(hAx(1),'Xlim',[0 1050000])
set(hAx(2),'Xlim',[0 1050000])
set(hAx(1),'Xtick',0:50000:1050000)
set(hAx(1),'XTickLabel',{'0','','100000','','200000','','300000','','400000','','500000','','600000','','700000','','800000','','900000','','1000000',''})
set(hAx(1),'Ylim',[-2 38])
set(hAx(1),'YTick',-2:2:38)
set(hAx(2),'YTick',0:20:400)
set(hAx(2),'Ylim',[0 400])
grid on
hold on 
plot (ZR,diff_L1,'color','g')
hold off
legend('Differenzumdrehungen PL2','Differenzumdrehungen PL1','Antriebsmoment')

For each section i need less then 1 or 2 sec. just for the Plot i need a bit more, but as i wrote 15 sec. tops it is nowhere near to 72 sec. that i get when i Run the Function. Thank you in advance.

Community
  • 1
  • 1
GDD
  • 11
  • 5

2 Answers2

0

Does your code improve it's performance when:

  1. you change indexes i to ii. i and j are reserved for imaginary units;
  2. define b=nan(length(a),1); and L1=zeros(length(test_L),1); and ommit b(:,end)=[]; and L1(iL:end)=[];;
  3. use for ii=1:length(a) instead of while loop;
  4. use b=zeros(length(a),1);b(a==1)=1; instead of whole while loop;
  5. use diff_L=L1-R1 instead of while loop;
  6. use Drehmoment=ones(l,1)*380;Drehmoment(1:100000)=100;?

These are just suggestions where you can loose your time...


EDIT regarding to hbadert's comment

Have you tried:

Block=1;
BlockTic(Block)=tic;
%%Block 1
BlockToc(Block)=toc;
Block=Block+1;
BlockTic(Block)=tic;
%%...
BlockToc(Block)=toc;

AllTime=BlockToc(end)-BlockTic(1);
Times=BlockToc-BlockTic;

disp(['Global time: ',num2str(AllTime)])
disp('Block times:')
disp(Times')
Crowley
  • 2,279
  • 6
  • 22
  • 36
  • I think you don't understand my question. I hope you see the different Sections, if i run just a section any of them they take 0,5 sec. i'm pretty sure they are as fast as they can be. I can run the whole code in under 10sec if i run them "sectionwise" and if i run the whole program it goes slowly in 72 secs to be exact. I do not think it has anything to do with the name of my variables. @Crowley thx i use 4 instead of my loop :) 5 is not bad idea, i use the loop because the length of LR and ZR is not the same. – GDD Apr 27 '16 at 11:22
  • Using `ii` instead of `i` is like driving on the right side of the road (unless you are japaneese, english,...). You can drive a car on the left side without any problem for years; untill you meet someone driving against you... – Crowley Apr 27 '16 at 12:31
  • 2nd and 6th poit may improve readability and may reduce memory usage because of proper allocation of variables. – Crowley Apr 27 '16 at 12:33
  • Why are `LR` and `ZR` of different dimension? Maybe you can use `diff(LR(1:M),ZR(1:M))` where `M=min([length(LR),length(ZR)]);` – Crowley Apr 27 '16 at 12:36
  • You also don't need to use unique counters for every loop, you can use only `ii`, `jj`, `kk` counters for whole `m-file`. – Crowley Apr 27 '16 at 12:37
  • Yeah, i use 2 now also :), but the length is different because they are different values of a measurement. I measure 3 sets of Voltage values with 3 sensors, 2 of them are length measurement L1, L2 and 1 is inductive measurement ZR (35million values pro set). I calculate the revolution with the aid of the voltage difference for L1,2 and calculate the difference between them and ZR. There is a first part of the program where i find my peaks and then i save them as "1" in test_L,R and a. Than I calculate the revolutions on the basis of the "1" and "0". – GDD Apr 27 '16 at 12:46
  • I used the same counter, but it is work in progress so i changed it to find easier what is what in my code because i didn't have time to put comments :). – GDD Apr 27 '16 at 12:48
  • Try my edit and run it in standard mode without any debugging and measuring tools - as @hbaderts suggested. What is the result? – Crowley Apr 27 '16 at 12:50
  • Oh and btw since I use your suggestion 2. i don't have "i" anymore :). But thx i will use other then "i" and "j" from now on. – GDD Apr 27 '16 at 12:54
  • i could not make it work yesterday and today i had no time to try again :). But thanks anyway. I know that the skript is fast enough and the problem was in the run and time :). – GDD Apr 28 '16 at 15:01
0

I've found the Problem, if i start the program with run and time, it tells me 72 sec, or after i used hint 4. from Crowley 60 sec. It also says that the "Run and Time fkt" don't use resources but if i just Run the program it completes in under 10sec.

GDD
  • 11
  • 5
  • 2
    With the MATLAB Profiler ("Run and Time"), the Just-in-Time Compiler (JIT) can not optimize certain things, such as `for`/`while` loops, which *can* make your code much slower. – hbaderts Apr 27 '16 at 12:15