Arranging arguments for speed
Joe Armstrong
joe@REDACTED
Mon Mar 24 13:03:42 CET 2003
erlc -S ... works even better :-)
I tried this with
f(X,alpha) -> a;
f(X,bravo) -> b;
f(X,zebra) -> c.
g(alpha,X) -> a;
g(bravo,X) -> b;
g(zebra,X) -> c.
Looking in the object code file
You'll see it generated
{function, f, 2, 2}.
{label,1}.
{func_info,{atom,test},{atom,f},2}.
{label,2}.
{test,is_atom,{f,1},[{x,1}]}.
{select_val,{x,1},
{f,1},
{list,[{atom,zebra},
{f,3},
{atom,bravo},
{f,4},
{atom,alpha},
{f,5}]}}.
...
and
{function, g, 2, 7}.
{label,6}.
{func_info,{atom,test},{atom,g},2}.
{label,7}.
{test,is_atom,{f,6},[{x,0}]}.
{select_val,{x,0},
{f,6},
{list,[{atom,zebra},
{f,8},
{atom,bravo},
{f,9},
{atom,alpha},
{f,10}]}}.
...
In other words - logically *identical* code - which is as it should be.
/Joe
On Mon, 24 Mar 2003, Thomas Lindgren wrote:
>
> --- Roger Price <rprice@REDACTED> wrote:
> > Given a function such as
> >
> > f(X,alpha) -> ...;
> > f(X,bravo) -> ...;
> > .....
> > f(X,zebra) -> ... .,
> >
> > would it run faster if re-written as
> >
> > g(alpha,X) -> ...;
> > g(bravo,X) -> ...;
> > .....
> > g(zebra,X) -> ... . ?
> >
> > It was certainly the case for a Prolog system I once
> > used, and I wondered
> > if Erlang also used a hash of the function name and
> > the first argument to
> > find the clause.
>
> There is luckily no need for that. The BEAM compiler
> uses a more flexible algorithm than most Prologs. (The
> reason is roughly that unification is hairier than
> pattern matching, so one normally has to be more
> conservative in a Prolog compiler.)
>
> Try 'erl -S foo.erl' to get the BEAM code for foo and
> see if it looks right.
>
> Best,
> Thomas
>
>
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!
> http://platinum.yahoo.com
>
More information about the erlang-questions
mailing list