a function slows down my profiler: I would like to speed it up

조회 수: 8 (최근 30일)
Luca Re
Luca Re 2024년 10월 21일
댓글: Luca Re 2024년 10월 22일 8:29
%{
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
Elapsed time is 1.038010 seconds.

답변 (1개)

the cyclist
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
Walter Roberson 2024년 10월 22일
idx = (id+1) - find(Ntrades(1:id, x), 1, 'last')
might possibly be faster.
Luca Re
Luca Re 2024년 10월 22일 8:29
yes..it's more fast but it's not equal

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by