[erlang-questions] Anonymous functions and performance
Filipe David Manana
Tue Nov 1 18:38:08 CET 2011
On Tue, Nov 1, 2011 at 5:32 PM, Ahmed Omar <spawn.think@REDACTED> wrote:
> I would expect the later to be more efficient as it's just a symbolic
> reference to the function (and it'll also all the time call the latest
> version of your module unlike anonymous fun)
> Also you can test it yourself like this
> 33> F1 = fun lists:sort/1.
> 34> F = fun(A)-> lists:sort(A) end.
> 35> F1 = fun lists:sort/1.
> 36> timer:tc(erlang, apply, [F, [[2,3,5,1,10,9,8]]]).
> 37> timer:tc(erlang, apply, [F1, [[2,3,5,1,10,9,8]]]).
> Anyone shall correct me if i'm wrong please :)
I did that same sort of testing as well before, in the shell. However
it's flawed, as the anonymous function is being interpreted and is not
compiled into beam byte code.
If you try compiling it, by moving all that into a module, perhaps you
get about the same timing results as I got with timer:tc/3.
> On Tue, Nov 1, 2011 at 6:02 PM, Filipe David Manana <fdmanana@REDACTED>
>> Is there any difference, regarding performance/efficiency, between the
>> 2 following calls:
>> F = fun(A, B) -> myfun(A, B) end,
>> F(foo, bar).
>> F = fun mymodule:myfun/2,
>> F(foo, bar).
>> I've heard about the later being more efficient, but haven't been able
>> to measure it (using timer:tc/3). Or is this part of the eight myths
>> (http://www.erlang.org/doc/efficiency_guide/myths.html) ?
>> Filipe David Manana,
>> "Reasonable men adapt themselves to the world.
>> Unreasonable men adapt the world to themselves.
>> That's why all progress depends on unreasonable men."
>> erlang-questions mailing list
> Best Regards,
> - Ahmed Omar
> Follow me on twitter
> erlang-questions mailing list
Filipe David Manana,
"Reasonable men adapt themselves to the world.
Unreasonable men adapt the world to themselves.
That's why all progress depends on unreasonable men."
More information about the erlang-questions