[erlang-questions] Anonymous functions and performance

Filipe David Manana fdmanana@REDACTED
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.
> #Fun<lists.sort.1>
> 34> F = fun(A)-> lists:sort(A) end.
> #Fun<erl_eval.6.80247286>
> 35> F1 = fun lists:sort/1.
> #Fun<lists.sort.1>
> 36> timer:tc(erlang, apply, [F, [[2,3,5,1,10,9,8]]]).
> {11,[1,2,3,5,8,9,10]}
> 37> timer:tc(erlang, apply, [F1, [[2,3,5,1,10,9,8]]]).
> {4,[1,2,3,5,8,9,10]}
> Anyone shall correct me if i'm wrong please :)

Thanks Ahmed.
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>
> wrote:
>>
>> Is there any difference, regarding performance/efficiency, between the
>> 2 following calls:
>>
>> 1)
>>
>> F = fun(A, B) -> myfun(A, B) end,
>> F(foo, bar).
>>
>>
>> 2)
>>
>> 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) ?
>>
>> Thanks
>>
>> --
>> 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
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
> --
> Best Regards,
> - Ahmed Omar
> http://nl.linkedin.com/in/adiaa
> Follow me on twitter
> @spawn_think
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>



-- 
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 mailing list