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?

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 Comments

Why would you just redefine two anonymous functions over and over again in a loop? That's just crazy. Makes no sense. You never even use f, and F would just end up leaving the loop with the last form that you assign to it in the very last iteration.
It seems like the speed difference is mainly because you're defining and redefining 2*length(y) anonymous functions then evaluating them. Have you tried using profile then profile viewer to see which parts are slow? I'll bet it's this issue.
I agree with Image Analyst though, the algorithm makes absolutely no sense in the first place, though.
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.
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.

Answers (1)

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 Comment

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?

This question is closed.

Asked:

on 25 Feb 2016

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!