- You call it as last branch of the switch cases. This needs addition time for 3 comparisons.
- You do not only call the anonymous function, but define it in addition in each iteration. The parsing and creation of the function takes time. Therefore this is not a fair comparison.
- I expect the evaluation of an anonymous function to be slower than a function call in general. I do not know the reason for this. Perhaps a missing JIT acceleration? But this is pure guessing. I usually do not care about why it is slower, but use anonymous functions only, if there are other good reasons to prefer them.
why is the anonymous function slower than a function call
20 views (last 30 days)
Show older comments
I have the following test. I run a for loop and use it to populate the sigma value of a gaussian distribution. I then either call a function or evaluate the expression directly or call a function handle or use an anonymous function. The latter is the slowest. Why?
Here is my code for the test:
% define inputs
kw.m1 = 0; % mean of gaussian
kw.A1=1; % amplitude of gaussian
x = -500:1:500; % timeline
% prepare for loop
tic
for i = 1:10:100
kw.s1 = i;
% in a for next loop generate different standard deviations for gaussians
% call functions using one of several options
switch callopt
% call function using handle
case 1
feval(@gfun,x,kw.m1,kw.A1,kw.s1);
% call function using name
case 2
gfun(x,kw.m1,kw.A1,kw.s1);
% simply use a for next loop
case 3
kw.A1*exp((-(x-kw.m1).^2)/kw.s1^2);
% anonymous
case 4
gauss = @(inp1,inp2,inp3,inp4) inp1*exp((-(inp2-inp3).^2)/inp4^2);
gauss(kw.A1,x,kw.m1,kw.s1);
end
end
runtime = toc;
and here is my function gfun
function [y] = gfun(x,m,A,s)
% create gaussian
y = A*exp((-(x-m).^2)/s^2);
0 Comments
Accepted Answer
Jan
on 15 Feb 2018
Some ideas:
2 Comments
Jan
on 19 Feb 2018
@schak: switch does the following:
- evaluate the argument
- compare it with 1
- compare it with 2
- compare it with 3
- compare it with 4
- run the code in the last case branch.
The comparisons are fast, but they take some time. Therefore the 1st branch is processed slightly faster than the last one. I assume that this needs some microseconds only, but you did not provide absolute timings and maybe it is worth to consider this additional delay.
Try this:
kw.m1 = 0; % mean of gaussian
kw.A1 = 1; % amplitude of gaussian
x = -500:1:500; % timeline
tic;
for i = 1:10:100
kw.s1 = i;
for k = 1:1000
dummy = feval(@gfun,x,kw.m1,kw.A1,kw.s1);
end
end
toc
tic;
for i = 1:10:100
kw.s1 = i;
for k = 1:1000
dummy = gfun(x,kw.m1,kw.A1,kw.s1);
end
end
toc
tic;
for i = 1:10:100
kw.s1 = i;
for k = 1:1000
dummy = kw.A1*exp((-(x-kw.m1).^2)/kw.s1^2);
end
end
tic;
gauss = @(inp1,inp2,inp3,inp4) inp1 * ...
exp((-(inp2-inp3).^2)/inp4^2);
for i = 1:10:100
kw.s1 = i;
for k = 1:1000
dummy = gauss(kw.A1,x,kw.m1,kw.s1);
end
end
toc
timeit would be even more accurate under a certain point of view. But it might be confusing, that it accepts the input as anonymous function also, but this is what you are interested in.
More Answers (1)
schak
on 19 Feb 2018
2 Comments
Adam Wyatt
on 6 Dec 2020
I agree with Jan, arrayfun (and cellfun) are extremely slow compared to direct loops, even in the latest version (R2020b). This may be because the for loop is optimised with the JIT compiler, whereas arrayfun has not had as much optimisation.
I also agree with Jan that arrayfun can make your code much nicer (i.e. easier to read/understand) since you it is clear what function the iteration is being applied to. It would be nice if arrayfun was optimised, and even had parallel support built it (oh well).
However, sometimes arrayfun is messier. When multiple functions need to be applied, a for loop can also be easier to understand.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!