Substitute out anonymous function
6 views (last 30 days)
Show older comments
To make my code readable, I use lots of anonymous function and nest them together. Imagine a simple example like this:
demand=@(p) a-b*p;
supply = @(p)c+d*p;
at this point a,b,c and d can be changed.
overhang = @(p) demand(p)-supply(p);
crit_f = @(p) overhang(p)^2;
and then use a solver on crit_f to find the price which clears the market.
However, I have now discovered that significant time is spent in "Self time". Using inline functions should help to speed the code up.
My question is whether there is a way to convert a complicated nested anonymous function into an inline function? I found func2str function, which I could use and "substitute out" the individual anonymous functions, but I do not know how to get access to the the objects stored within anonymous functions (this this example parameters a,b,c and d).
4 Comments
Philip Borghesani
on 4 Oct 2018
What version of matlab are you using? Versions after 2015b have significantly better performance with anonymous functions (Which perform better then inline functions... see my comment below) It could be that upgrading matlab would solve your issues
Walter Roberson
on 5 Oct 2018
Multiple levels of anonymous functions are slow, seemingly unreasonably so. In releases before R2018b you should avoid them for code that needs to be high performance.
Philip, you might want to look at my case 02665303 where I submitted code that proved this against R2017b, with Mathworks having acknowledged a bug; I was told the problem was fixed for R2018b. There was a follow-up case 03222935 against R2018b which Mathworks said was due to CPU cache effects, but when I submitted code that should not have had meaningful cache effects to demonstrate the problem still existed, I did not receive any reply. The cache part focused on the nested functions case but I did not seem to see any timing improvement for anonymous functions.
Accepted Answer
Steven Lord
on 4 Oct 2018
It is possible to obtain information about the values used to create the anonymous function for debugging purposes, but you should not use the tool that does this as part of your normal workflow. There's even a note to that effect in its documentation.
Create an anonymous function then destroy the data used to make it
>> a = 42;
>> f = @(x) a+x;
>> clear a
You can see that the variable a no longer exists in the workspace.
>> whos
Name Size Bytes Class Attributes
f 1x1 32 function_handle
Use the functions function to obtain information about f.
>> S = functions(f);
The workspace information stored in S contains information about the value the anonymous function f "remembers".
>> S.workspace{1}
ans =
struct with fields:
a: 42
If by "inline function" you mean you want to create an inline object, I strongly recommend against doing that. I'm not sure your belief that "Using inline functions should help to speed the code up." is correct.
Note that "self time" is not necessarily a bad thing; from the documentation it is "Total time in seconds spent in a function, excluding time spent in any child functions. Self time also includes some overhead resulting from the process of profiling." Given the simple functions you've posted, the time required to perform addition, subtraction, and multiplication probably all show up as "self time". You can't avoid that by moving to the very old inline object.
7 Comments
Stephen23
on 5 Oct 2018
"...inlining your code (function/script) into the calling loop"
I also find the terminology in that thread confusing. It should be clarified.
Steven Lord
on 5 Oct 2018
Yes, I see your point about the wording of that Answers post. I'll ask the Support Team to update it.
More Answers (2)
James Tursa
on 4 Oct 2018
E.g.,
funs = functions(demand);
funs.workspace{1} <-- gets the embedded data in the demand function handle
0 Comments
Walter Roberson
on 4 Oct 2018
In some cases a viable route is to use the symbolic toolbox, declaring a symbolic variable or vector, calling the last level function on it, getting back the expression, and using matlabFunction on it, especially with the 'file' option with common sub expression optimization turned on.
0 Comments
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!