필터 지우기
필터 지우기

Similar lines are much different in time for run due to MATLAB profiler.

조회 수: 2 (최근 30일)
hosein bashi
hosein bashi 2022년 1월 13일
답변: Philip Borghesani 2022년 1월 20일
The right hand side of all of below equation are numbers (in same order of magnitude) but the first line is much slower. Any idea why this is happening? The image of profile is also attached.
% Comp.Yp(j,1)=Yp;
% Comp.Yp1(j,1)=Yp1;
% Comp.Yp2(j,1)=Yp2;
% Comp.Yt(j,1)=Yt;
% Comp.Y_sh(j,1)=Y_sh;
% Comp.Y_Ex(j,1)=Y_Ex;
  댓글 수: 3
hosein bashi
hosein bashi 2022년 1월 13일
I have a very big code. I do not know much about using classes but using class definition helped me making the code more neat. So I used static method to have functions in different classes to remmeber functions and find them quickly when they are neatly organized. here is a part of one of the satic method function:
function [Comp]=Loss_SL(Comp,QO,n,j,Nmid)
%input:Geometry;betaBC1;Machr1,2;alphaBC1,2;
sc=Comp.G.sc(j,1);
c=Comp.G.c(j,1);
s=sc.*c;
o=Comp.G.o(j,1);
e=Comp.G.e(j,1);
te=Comp.G.te(j,1);
t=Comp.G.t(j,1);
sRc=Comp.G.sRc(j,1);
.....
Doing many Calculations whith above parameters and compute Yp,Ys, ... and save them in Comp object:
....
Comp.Yp(j,1)=Yp;
Comp.Yp1(j,1)=Yp1;
Comp.Yp2(j,1)=Yp2;
Comp.Yt(j,1)=Yt;
Comp.Y_sh(j,1)=Y_sh;
Comp.Y_Ex(j,1)=Y_Ex;
Philip Borghesani
Philip Borghesani 2022년 1월 20일
You are calling this funciton quite a few times. Without more understanding of your code it is unlikely that a good anwer can be supplied. Some questions and things to think about:
  1. Is your object a handle or value object? If it is simple give some info about it or the code.
  2. How are you calling this function? what are the inputs and outputs?
  3. Are your members (Yp1,Yp2,...) preallocated?
Most likely the best way to improve your code is to restructure it so that the helper function can be vectorized and not called so many times.

댓글을 달려면 로그인하십시오.

답변 (2개)

Fangjun Jiang
Fangjun Jiang 2022년 1월 13일
It takes time to create "Comp" as a structure. Add the line below first and then profile again.
Comp=struct;
  댓글 수: 1
hosein bashi
hosein bashi 2022년 1월 13일
Thank you. But as I explained for Rik in previous comment the Comp is an object which is input (and output) of a function. so I think it's previously an structure.

댓글을 달려면 로그인하십시오.


Philip Borghesani
Philip Borghesani 2022년 1월 20일
Most likely this is happining due to memory allocation, caching and array growth. Unfortunatly this answer wont help you much.... See my comments to your question

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by