[erlang-questions] Anonymous functions and performance
Wed Nov 2 11:08:46 CET 2011
To start with you are not measuring what you think you are measuring.
The code below measures a mixture of several things:
1) The time it takes the interpretor to interpret the code you have typed in
2) The time spent in the compiled code for lists:sort/1
I expect the time is dominated by sort and not setting up the call to sort.
Creating a fun efficiently is a compiler writers concern - just assume
it is done as fast as possible.
Once created the time to call it should not depend upon how it was
created. It should be the same
and as efficient as possible. So you are really asking "which of the
two methods of creating a fun is
the most efficient"
Now suppose there are two ways of creating a fun (a) and (b) and I say
"(a) is more efficient" - what do you do?
if you write all your code using (a) - then the compiler writer might say
"that's funny, there is something wrong with (b) so I'll speed this up.
You might like to write all your code using (b) so if it's not
efficient enough you can make a new
version using (a)
Whatever you choose there is no guarantee that your decision will be
true in the future.
Efficiency is obtained by choosing good algorithms.
I always ask "which is the most beautiful code" - I try to write the
code that is the easiest possible to understand
In the extremely rare case that the code is not efficient enough - I
write the application, then measure, then fix
At at guess I'd say less than 5% of all programs I've every written
needed optimizing. And of these
5% I guess 95% (of the 5%) had problems in I/O - ie 0.25% of all code
needs its I/O to be tweaked.
25 years ago machines had clock frequencies of a few MHz and memories under 1MB.
Now machines have several GHz clock frequencies and GBytes of memory.
When the great old ones said years ago "Premature optimization
is the root of all evil" - they were using machines that by todays
incredibly slow. Even then they viewed premature optimization as evil.
On Tue, Nov 1, 2011 at 6:32 PM, Ahmed Omar <> 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 :)
> On Tue, Nov 1, 2011 at 6:02 PM, Filipe David Manana <>
>> 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
More information about the erlang-questions