Why ist my variable allocation so slow?

3 次查看(过去 30 天)
At the beginning of my function i am assigning values to variables.
When analysing the function with the profiler, it seems that two lines are are a lot slower that the rest. Does anybody know the reason for this? How can I speed up these lines?
I do not understand thes especialy since the contents of the varibles pn and an (lines 22 and 19) are quite similar, but the time needed is 50 times slower. An example or an and pn are:
How chan the time needed be so different?
  2 个评论
Rik
Rik 2021-4-23
The results for an and pn might be similar, but why would that mean the time required to get there would be similar? Those two lines look like they're doing very different things. Indexing several layers deep is bound to get slow at some point. You could try storing each step in an intermediate variable to see which of these step are the true bottleneck.
Mathias Dehn
Mathias Dehn 2021-4-23
编辑:Mathias Dehn 2021-4-23
Hi Rik,
thank you for your Comment. Is there a general best practice on how to store data so that I can access it quickly?

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2021-4-23
Do not overestimate the accuracy of the profiler. Sometimes the END uses a lot of time in a FOR loop, although no work is done there.
Matlab's JIT can reorder commands and re-use deeply nested arrays. This confuses the profiler massively and important or all parts of the JIT acceleration are switched off for profiling. So you have to interprete the results with care.
The code can be accelerated by avoiding repeated access of deeply nested structs. Please post the corresponding code, such that we can suggest improvements by copy&paste.
  1 个评论
Mathias Dehn
Mathias Dehn 2021-4-23
Hello Jan,
thank you for your quick reply, I did not know that the profiler can be inaccurate. Here ist the complete code of the function:
function [u,SPINDLE] = FStoS_Target_PosDep(SPINDLE,SETUP,t,contact)
%--------------------- relevant constants ----------------------
% alpha = 500;%SPINDLE.CONSTANTS.alpha_BG2BG1_KB1;
per = SETUP.per;
% alphaq2 = SETUP.alphaq2(per);
alphaq2 = SETUP.alphaq2;
time = SETUP.time;
R = SETUP.P_RQ(per);
V = SPINDLE.POSITION.VELOCITY_smooth((per-1)*time + t,:);
% V = SPINDLE.POSITION.VELOCITY{per}(t,:);
%-----------------------------------------------------------------
bg_active = SPINDLE.ABSTRACT.CONTACT{contact}.bg_active;
bg_passive = SPINDLE.ABSTRACT.CONTACT{contact}.bg_passive;
pin_active = SPINDLE.ABSTRACT.CONTACT{contact}.pin_active;
pin_passive = SPINDLE.ABSTRACT.CONTACT{contact}.pin_passive;
a_id = SPINDLE.ABSTRACT.CONTACT{contact}.a_id;
pn = SPINDLE.ABSTRACT.CONTACT{contact}.pn;
node_n = SPINDLE.BGs.BG_FEM{bg_passive}.info.node_n;
seg_active = SPINDLE.BGs.BG_FEM{1,bg_active}.PIN{1,pin_active}.seg_active{per}(t); %active Segment for time step t
an = SPINDLE.BGs.BG_FEM{1,bg_active}.PIN{1,pin_active}.POSITION_mask.Nodes{seg_active};
% aktive_mask =
% SPINDLE.BGs.BG_FEM{1,bg_active}.PIN{1,pin_active}.POSITION_mask.Mask{seg_active,1};
% %select active mask
% Eq2Nd_a = SPINDLE.BGs.BG_FEM{1,bg_active}.sys_mat_thm.Eq2Nd_therm;
Nd2Eq_p = SPINDLE.BGs.BG_FEM{1,bg_passive}.sys_mat_thm.Nd2Eq_therm;
T_active = mean(SPINDLE.BGs.BG_FEM{1,bg_active}.sys_mat_thm.Temp(an,per)); %T_active anhand der aktiven Knoten bestimmen
T_passive = mean(SPINDLE.BGs.BG_FEM{1,bg_passive}.sys_mat_thm.Temp(pn,per));
A = sum(SPINDLE.BGs.BG_FEM{bg_passive}.sys_mat_thm.B_therm(:,pin_passive));
dT = zeros(node_n,1);
dT(Nd2Eq_p(pn)) = (T_active-T_passive);
q = zeros(node_n,1);
q(Nd2Eq_p(pn)) = 0.5*(100* V(a_id)*R/A);
% Q(aktive_mask) = 0.5*(100* V(a_id)*R/A);
u = alphaq2*dT + q;

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Historical Contests 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by