Why ist my variable allocation so slow?

3 Ansichten (letzte 30 Tage)
Mathias Dehn
Mathias Dehn am 23 Apr. 2021
Bearbeitet: Mathias Dehn am 23 Apr. 2021
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 Kommentare
Rik
Rik am 23 Apr. 2021
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 am 23 Apr. 2021
Bearbeitet: Mathias Dehn am 23 Apr. 2021
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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jan
Jan am 23 Apr. 2021
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 Kommentar
Mathias Dehn
Mathias Dehn am 23 Apr. 2021
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;

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Historical Contests finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by