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!