a function slows down my profiler: I would like to speed it up
조회 수: 8 (최근 30일)
이전 댓글 표시
%{
hi, i've create a large code..
it's fast but there is a function that slows everything down for me (my application does several simulations by executing this function several times)
I want to give it a try by asking someone experienced if it can be speeded up
it's function name "Calcola_MinTrade". and in profile it's that's what slows everything down
%}
RP_bin=load('matlab_RP_bin.mat');
MinNtrad=load('matlab_Ntrades.mat');
tic
RP_bin=RP_bin.RP_bin;
Ntradess=MinNtrad.Ntradess;
period=2;
minTrades=16;
z=find(RP_bin);
[~,c]=size(Ntradess);
MinNtrad=zeros(numel(z),c);
for x=1:c
for i=period+1:numel(z)
id=z(i);
a=Ntradess(id,x);
a1=a-minTrades+1;
%%******************
kk=z(max(1,i-period));
if ~minTrades
b=kk;
else
idx=find(flip(Ntradess(1:id,x))<=a1,1,'first');
b=id-idx+1;
b=min(b,kk);
%%*****************
end
if ~isempty(b)
MinNtrad(i,x)= b;
end
end
end
%the function return MinNtrad
toc
댓글 수: 0
답변 (1개)
the cyclist
2024년 10월 22일
It won't be a big speedup, but I would expect
idx=find(Ntradess(id:-1:1,x)<=a1,1,'first');
to be consistently faster than
idx=find(flipud(Ntradess(1:id,x))<=a1,1,'first');
(Note that I reversed the indexing in the first dimension.)
댓글 수: 2
Walter Roberson
2024년 10월 22일
idx = (id+1) - find(Ntrades(1:id, x), 1, 'last')
might possibly be faster.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!