long execution time for simple lines like end and vector value addition

3 views (last 30 days)
I wanted to make my code more time efficient so looked in the profiler and saw that the most time consuming lines are 'end' and another quite simple line. it is a long loop but other lines in it runs much faster. Does anyone know why they take so long or have an idea how ti improve this loop?
Thanks!
  1 Comment
Jan
Jan on 7 May 2018
Under Matlab R2016b/Win10_64/i7 I get
sec calls
0.08 2399800 Efoot(n)=Efoot(n-1)+dt*(qin(n-1)+qm_foot-qsk(n-1));
0.08 2399800 Esk(n)=Esk(n-1)+dt*(qsk(n-1)+qm_sk-qout(n-1));
In your example the input data are 4 times bigger, but ths code needs 240 times longer. This is surprising. The main work is done in findpeaks.
Which Matlab version are you using?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 7 May 2018
>> 19.30/10799100
ans =
1.78718596920114e-06
The iterations are not taking long; you are just doing a lot of them.
You are doing 2 additions, 4 subtractions, and 1 multiplication on the line that is taking the most time.
You could potentially reduce the time by assigning n-1 to a variable, but I would not be surprised if the Just In Time compiler is effectively already doing that internally.
  2 Comments
Walter Roberson
Walter Roberson on 7 May 2018
When MATLAB detects a code sequence that can be handled by the high performance libraries it calls into them. The code pattern might cross multiple lines so the required time cannot always be allocated to one line. MATLAB allocates it to the end statement instead.

Sign in to comment.

More Answers (2)

Jan
Jan on 1 May 2018
A very important part of the code is missing: Is Efoot and Esk pre-allocated properly? Otherwise the arrays grow iteratively, which is very expensive.
Analysing the run time behavior of a code, which is shown as screenshot and partially only, is a very bold task. I cannot guess e.g., if "qin" is an array or a function. Please post the code as text, such that it can be used by copy&paste, and add some meaningful test data - either by attaching them as MAT file or by some rand() commands with relevant sizes.

Oded Scharf
Oded Scharf on 1 May 2018
Edited: Oded Scharf on 7 May 2018
Hey, All of the vectors are pre-allocated and there are no inner functions in this function. Im attaching the code and the relavent parameters (one as an array and the other as a script to be run before the function) Thank you for your help! code:
function:
function [ Tsk_f, Tfoot_f,ssTsk,ssTfoot,skinrisetime, footrisetime,skp2p ] =...
newTempcalc(dt, Qfoot,roblood,Cblood,Cfoot,qm_foot,Rsk,Csk,qm_sk,Rh)
cycles=200;
%%setting start conditions
%cycle vectors
RQ=(roblood*Cblood*Qfoot).^-1;
Rout=ones(size(Qfoot))*Rh;
Tfoot=zeros(size(Qfoot));
Tsk=zeros(size(Qfoot));
Ta=ones(size(Qfoot))*37;
Troom=ones(size(Qfoot))*24;
Efoot=ones(size(Qfoot))*Tfoot(1)*Cfoot;
Esk=ones(size(Qfoot))*Tsk(1)*Csk;
qin=zeros(size(Qfoot));
qsk=zeros(size(Qfoot));
qout=zeros(size(Qfoot));
%full vectors
Efoot_f=zeros(1,cycles*length(Qfoot));
Esk_f=zeros(1,cycles*length(Qfoot));
Tfoot_f=zeros(1,cycles*length(Qfoot));
Tsk_f=zeros(1,cycles*length(Qfoot));
qin_f=zeros(1,cycles*length(Qfoot));
qsk_f=zeros(1,cycles*length(Qfoot));
qout_f=zeros(1,cycles*length(Qfoot));
%%iterations
for cyc=1:cycles %loop for every cycle
for n=2:length(Qfoot) % lop for every time step
%updating enrgy stored in capacitors
Efoot(n)=Efoot(n-1)+dt*(qin(n-1)+qm_foot-qsk(n-1));
Esk(n)=Esk(n-1)+dt*(qsk(n-1)+qm_sk-qout(n-1));
%updating tissues temp
Tfoot(n)=Efoot(n)/Cfoot;
Tsk(n)=Esk(n)/Csk;
%updating heat transfer through resistors
qin(n)=(Ta(n)-Tfoot(n))/RQ(n);
qsk(n)=(Tfoot(n)-Tsk(n))/Rsk;
qout(n)=(Tsk(n)-Troom(n))/Rout(n);
end
% cycling variables
start=(cyc-1)*length(Qfoot)+1;
stop=cyc*length(Qfoot);
Efoot_f(start:stop)=Efoot;
Esk_f(start:stop)=Esk;
Tfoot_f(start:stop)=Tfoot;
Tsk_f(start:stop)=Tsk;
qin_f(start:stop)=qin;
qsk_f(start:stop)=qsk;
qout_f(start:stop)=qout;
Efoot(1)=Efoot(length(Qfoot));
Esk(1)=Esk(length(Qfoot));
Tfoot(1)=Tfoot(length(Qfoot));
Tsk(1)=Tsk(length(Qfoot));
qin(1)=qin(length(Qfoot));
qsk(1)=qsk(length(Qfoot));
qout(1)=qout(length(Qfoot));
end
ssTsk=mean(Tsk_f(length(Tsk_f)-10000:end));
ssTfoot=mean(Tfoot_f(length(Tfoot_f)-10000:end));
[~,skinrisetime] =min(abs(Tsk_f-(0.63*(ssTsk-Tsk_f(1))+Tsk_f(1))));
[~,footrisetime] =min(abs(Tfoot_f-(0.63*(ssTfoot-Tfoot_f(1))+Tfoot_f(1))));
skinrisetime=skinrisetime*dt;
footrisetime=footrisetime*dt;
%risetimeval=Tsk_f(risetime);
[~, maxindex]=findpeaks(Tsk_f);
[~, minindex]=findpeaks(-Tsk_f);
if isempty(maxindex) || isempty(minindex)
skp2p=0;
else
skp2p=Tsk_f(maxindex(end))-Tsk_f(minindex(end));
end
end
script:
params
[ Tsk_f, Tfoot_f,ssTsk,ssTfoot,skinrisetime, footrisetime,skp2p ] =...
newTempcalc(dt, Qfoot,roblood,Cblood,Cfoot,qm_foot,Rsk,Csk,qm_sk,Rh)
  3 Comments
Oded Scharf
Oded Scharf on 7 May 2018
I uploaded the array Qfoot seperatly. I didn't understand what is the JIT acceleration.
thank you.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!