필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

I wrote 2 snippets as below, they both try to find minimum value, but one repetitively uses function handles and the other uses a sub function. It turns out the speed are very different? Why?

조회 수: 1 (최근 30일)
Two snippets are as follow (they are not my original codes and are used for illustration only), the input y is just an array, for example, you can run method1(rand(500,1)) and method2(rand(500,1)), respectively. Interestingly, although both do the same job, when y is very large, method1 would take unbearable long time but method2 works pretty fine. What makes this huge difference?
Code 1
function out=method1(y)
f=@(x)0;F=@(x)0;
for i=1:length(y)
f=@(x)x(1)*f(x)^2+x(2)*y(i)^2;
F=@(x)x(1)*F(x)+x(2)*log(f(x)^2+1)-10*y(i)^3;
end
options=optimoptions(@fminunc,'Algorithm','quasi-newton');
out=fminunc(F,[0,0],options);
end
Code 2
function out=method2(y)
options=optimoptions(@fminunc,'Algorithm','quasi-newton');
out=fminunc(@(x)subfun(x,y),[0,0],options);
end
function F=subfun(x,y)
f=0;F=0;
for i=1:length(y)
f=x(1)*f^2+x(2)*y(i)^2;
F=x(1)*F+x(2)*log(f^2+1)-10*y(i)^3;
end
end
  댓글 수: 4
Shawn Miller
Shawn Miller 2016년 2월 25일
편집: Shawn Miller 2016년 2월 25일
I got this question when I tried to recode MATLAB's built in estimate(mdl,y) function used in GARCH model. As I said, my code shown here is just for illustration, it has no sense. However, it may has some sense in some way. Please note that in the loop, f is redefined but did use previous f(x) value, and F is redefined and also used previous F(x) and f(x) value. In other words, one can't write final F(x) directly. The final F(x) is derived step by step -- it's a recurrence relation. In terms of x, because it's to be estimated using fminunc(), so I need to write it using a function handle.
I guess the slow speed in method1 may be due to function handle. But I am not sure and wonder if function handle performs poor in speed and needs to be avoided whenever it's possible.
Shawn Miller
Shawn Miller 2016년 2월 25일
편집: Shawn Miller 2016년 2월 25일
Also think in this question, I have a column of number a1,a2,...,an; and a column of functions b1(x),..., bn(x). How do I get these functions? Well, I used relations as below b1(x)=f(a1,x),b2(x)=f(a1,b1(x)),b3(x)=f(a2,b2(x)),...,bn(x)=f(an-1,bn-1(x)). Now I'd like to find x such that g(b1(x),...,bn(x)) reaches minimum, g is just a function on these b functions. In brief, one has to use a loop to first get every b function, and then plug in g() and treat x as unknown and find minimum.

답변 (1개)

Philip Borghesani
Philip Borghesani 2016년 2월 25일
Profile your code, think about what the code is doing. Method2 makes one function call to subfun per iteration. Method1 makes many thousands of function calls using the many anonymous functions you have created per iteration. There is no comparison in the amount of work being done. Recursive solutions are rarely the most efficient solution to a problem.
I do find that Method1 runs significantly faster in R2015b then in previous versions but it is still much slower then Method2.
  댓글 수: 1
Shawn Miller
Shawn Miller 2016년 2월 26일
How do I profile a function? Also, when you said "Method2 makes one function call to subfun per iteration", what do you mean by "per iteration"? Do you mean every time fminunc() tries a point to see if it's minimum?

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by