[erlang-questions] Efficiency is passing a functin

Ulf Wiger ulf@REDACTED
Sun Sep 23 14:55:49 CEST 2012

To begin with, your example won't compile, but the question is still valid.

Consider this program:

-export([f1/2, f2/2]).

f1(Acc, Lst) ->
    F = fun(A,B) -> my_function(A,B) end,
    lists:foldl(F, Acc, Lst).

f2(Acc, Lst) ->
    lists:foldl(fun my_function/2, Acc, Lst).

my_function(X, Acc) ->

I believe this illustrates the question, but compiles and runs correctly.

Eshell V5.9  (abort with ^G)
1> foldtest:f1([], [1,2,3]).
2> foldtest:f2([], [1,2,3]).

Now, let's compile the code with `erlc -S foldtest.erl` (generating ASM code):

{function, f1, 2, 2}.

{function, f2, 2, 4}.

As you can tell, the two alternatives generate identical code.

The fun is compiled into a regular function, albeit with a funny name, and calling it carries the same cost as calling a hand-written function. Actually instantiating a fun context carries a slight overhead depending on the number of variables that need to be imported into the context (in this case, zero). There will be one level of indirection in both cases.

Ulf W

On 23 Sep 2012, at 02:07, James Rosenblum wrote:

> Is there an appreciable difference in these two ways of passing a function?
> F = fun(A,B) -> my_function(A,B) end.
> lists:map(F, Lst)
> vs.
> -export([my_function/2]).
> lists:map(fun my_function/2, Lst)

Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120923/09a5c7de/attachment.htm>

More information about the erlang-questions mailing list