Will there be a performance difference between anonymous functions and normal functions?

Will there be a performance difference between anonymous functions and normal functions? For example, any change in the overhead of a function call?

Thank you and welcome!

+5
source share
2 answers

Unfortunately, I could not find anything specific on this subject. However, anonymous functions must have additional overhead compared to regular functions.

You can try it yourself. First create a filenonanon.m

function x=nonanon(y)
  x=y^2;
end

Then create the cell file with:

%% non anon
tic
for i=1:1000000
    z=nonanon(i);
end
toc

%% anon
f=@(x) x^2;
tic
for i=1:1000000
    z=f(i);
end
toc

enjoy, exit:

Elapsed time - 0.513759 seconds.

The elapsed time is 14.434895 seconds.

Which concludes that anonymous functions are slower.

+9
source

user677656 , y=x*x ( nonanon anon):

Elapsed time is 0.517514 seconds.
Elapsed time is 0.223450 seconds.

y=x^2, user677656:

Elapsed time is 0.402366 seconds.
Elapsed time is 7.440174 seconds.

Matlab 2012b. , .

y=sin(x), , x*x, y=sqrt(x), (2,8 3,9 ) nonanon.

0

All Articles