why is the anonymous function slower than a function call

20 vues (au cours des 30 derniers jours)
schak
schak le 15 Fév 2018
Commenté : Adam Wyatt le 6 Déc 2020
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);

Réponse acceptée

Jan
Jan le 15 Fév 2018
Some ideas:
  1. You call it as last branch of the switch cases. This needs addition time for 3 comparisons.
  2. 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.
  3. 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.
  2 commentaires
schak
schak le 19 Fév 2018
Thanks Jan,
I agree with points 2 and 3 but shouldnt pt 1 be the same for the other call methods?
Best Shubo
Jan
Jan le 19 Fév 2018
@schak: switch does the following:
  1. evaluate the argument
  2. compare it with 1
  3. compare it with 2
  4. compare it with 3
  5. compare it with 4
  6. 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.

Connectez-vous pour commenter.

Plus de réponses (1)

schak
schak le 19 Fév 2018
I have a related question. Would arrayfun with the appropriate function handle be even faster?
  2 commentaires
Jan
Jan le 19 Fév 2018
Try it. In my experiences arrayfun is nicer, but slower than direct loops.
Adam Wyatt
Adam Wyatt le 6 Déc 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by