reduce execution time in matlab
Mostra commenti meno recenti
I have a matlab programm with a loop, it takes with a machine of 16GO RAM, 7 hours to be executed. The aim is to compute a cross-correlation coefficient on a moving window (for each 1000 observations we compute using a DCCA function a detrended cross correlation); The program is as follows:
X and Y are two vectors with same length (5000 observation), for a given n=10 (20,40,....)
ft=zeros(1000,1);
gt=zeros(1000,1);
FT=zeros(1000,1) ;
Gt=zeros(1000,1) ;
CovXY=zeros(length(X)-999,1) ;
VarX=zeros(length(X)-999,1) ;
VarY=zeros(length(X)-999,1) ;
rho=zeros(1,length(X)-999) ;
x=zeros(1000,1) ;
y=zeros(1000,1);
for j=1:length(X)-999
x=X(j :j+999);
y=Y(j :j+999) ;
[parameters, ll, Ft, VCV, scores] = garchpq(x,1,1) ;
clear parameters ll VCV scores
[parameters, ll, Gt, VCV, scores] = garchpq(y,1,1) ;
clear parameters ll VCV scores
for k=1 :1000
ft(k)=x(k)/sqrt(Ft(k));
gt(k)=y(k)/sqrt(Gt(k)) ;
end
[CovXY(j, :),VarX(j, :),VarY(j, :)]=DCCA(ft,gt,n);
rho(j)=CovXY(j)/(VarX(j)*VarY(j)) ;
end
end
I'm asking someone to help me, in order to reduce execution time, i tried preallocation. I don't know what can i do else. For one segment the program takes some second.
15 Commenti
dpb
il 10 Ott 2018
- What is garchpq?
- What is DCCA?
Couple of things as exist may make minor improvement are
- Don't need explicit clear
- ft=x./sqrt(Ft); % no need for loop for ft, gt
Maybe you don't need to use sqrt at all if normalization factor isn't necessary?
When know what the other functions are, there's also possibilities there perhaps for improvments as well as blockproc or filter maybe.
tilfani oussama
il 10 Ott 2018
dpb
il 10 Ott 2018
Well, we can't optimize what we can't see...
What does the result of profiler show are the bottleneck(s)?
tilfani oussama
il 10 Ott 2018
tilfani oussama
il 10 Ott 2018
Modificato: dpb
il 10 Ott 2018
OCDER
il 10 Ott 2018
Just wondering, is "Cov_" the same as the covariance, cov? And "Var_" the same as the variance, var?
https://www.mathworks.com/help/matlab/ref/cov.html https://www.mathworks.com/help/matlab/ref/var.html
There's also something for cross-covariance, xcov, and cross-correlation, xcorr.
https://www.mathworks.com/help/signal/ref/xcov.html https://www.mathworks.com/help/signal/ref/xcorr.html
Are these different from what you compute with DCCA?
tilfani oussama
il 10 Ott 2018
the cyclist
il 10 Ott 2018
the cyclist
il 10 Ott 2018
Modificato: the cyclist
il 10 Ott 2018
I don't think it will speed things up, but if you only need Ft and Gt, then you can call garchpq with this syntax:
[~, ~, Ft] = garchpq(x,1,1);
[~, ~, Gt] = garchpq(y,1,1);
tilfani oussama
il 10 Ott 2018
dpb
il 10 Ott 2018
Did you mean: garch0 (2 results)
No results for garchpq.
so I still don't know garchpq. There's a garch but it doesn't seem to have same footprint.
the cyclist
il 10 Ott 2018
Google search for "garchpq matlab" did not turn up anything for me.
tilfani oussama
il 10 Ott 2018
Greg
il 10 Ott 2018
You've ignored both suggestions to run the profiler. It is specifically designed to help you do exactly what you are trying to do - identify slow lines of code to try to speed them up.
We can guess what is slowing you down. The profile will know what is doing it.
dpb
il 11 Ott 2018
"Yes garch0 (p q) are parameters"
Well, that's not what' written in the code posted. Nor does the return match the profile...
Risposta accettata
Più risposte (1)
Bruno Luong
il 12 Ott 2018
Modificato: Bruno Luong
il 12 Ott 2018
0 voti
@dpb
The comment %#ok<AGROW> tells the author (Kevin Sheppard) is well aware about growing, and he obviously doesn't care.
The code garhcpq.m looks well written, contrary to the function DCCA() which is ugly IMHO.
5 Commenti
tilfani oussama
il 12 Ott 2018
Bruno Luong
il 12 Ott 2018
Modificato: Bruno Luong
il 12 Ott 2018
You need first to do what every one ask you: run a short number iteration with the PROFILER then post the result.
But if you miss my post, No I don't think there is a chance to accelerate the garhcpq.m
Some people focus on the growing stuff with K=1 in your example so there is no pb there.
The consumed time is FMINCON running by GARCHPQ over and over.
I didn't "FOCUS" on it; it was just the first thing that jumps out...
That the author didn't care is one thing; and it undoubtedly won't make any measurable difference overall, granted, but it is sloppy and easily remedied in this case since they are counted loops, not uncertain size.
It's probably because it is "only" initialization that the author left it but why not fix what can easily just on general principles?
Bruno Luong
il 12 Ott 2018
Modificato: Bruno Luong
il 12 Ott 2018
This loop is just for building the starting guess vector (composed by few segments) for FMINCON, and believe me this is next to nothing the work that would be carried out later.
The delta growing is not constant, granted can be computed without the loop growing. But there are few reasons why the author prefer coding that way:
- Because it's not important?
- The author prefer spending his time for something else?
- Because the code is more readable in this way?
- Because the code is more easily to be modified and maintainable in this way?
dpb
il 12 Ott 2018
I'm not arguing it would make any noticeable improvement; just stuck out at first glance so pointed it out is all...
As for the other three points all it would have taken is writing one line using an already-defined variable and modifications would have been automagic that way just as are as is...took almost as long for the author to acknowledge the "ignore" message as it would have to have fixed at the time.
A big deal? In this case, no. In general, poor coding practice is still poor coding practice.
Categorie
Scopri di più su Descriptive Statistics in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!