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.

3
  • What is Min_Zr? What size is it? Commented Apr 27, 2016 at 9:23
  • What are test_L and test_R? Commented Apr 27, 2016 at 9:50
  • Also good information is, what can be inside variables mentioned by @StewieGriffin. Are they logical, integer, float,...? Commented Apr 27, 2016 at 9:53

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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')

10 Comments

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.
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...
2nd and 6th poit may improve readability and may reduce memory usage because of proper allocation of variables.
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)]);
You also don't need to use unique counters for every loop, you can use only ii, jj, kk counters for whole m-file.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.