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
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.)

类别

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