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

7 vues (au cours des 30 derniers jours)
Luca Re
Luca Re le 21 Oct 2024
Commenté : Luca Re le 22 Oct 2024
%{
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.

Réponses (1)

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by