[erlang-questions] Blog: Erlang Fractal Benchmark
Thomas Lindgren
thomasl_erlang@REDACTED
Tue Jun 5 08:49:45 CEST 2007
--- Jim Menard <jim.menard@REDACTED> wrote:
> On 6/4/07, Ulf Wiger (TN/EAB)
> <ulf.wiger@REDACTED> wrote:
> > Adding is_float/1 guards around all
> > variables that are floats (quite a few) and
> > compiling with native brought it down to 1.8 secs.
>
> It sped up my version considerably as well. Why is
> that? I thought
> that a guard clause was only used to select which
> function clause to
> run. How does checking to see if something is a
> float make things
> faster instead of slowing it down by having to
> execute is_float/1 for
> each call?
It's a compiler heuristic. When the compiler knows it
is operating on a float, it can emit more specific
code.
Example:
-module(fp).
-compile(export_all).
dot1(X,Y,Z,A,B,C) ->
X*A+Y*B+Z*C.
dot2(X,Y,Z,A,B,C)
when is_float(X), is_float(Y), is_float(Z),
is_float(A), is_float(B), is_float(C) ->
X*A+Y*B+Z*C.
These functions are respectively compiled as:
{function, dot1, 6, 2}.
{label,1}.
{func_info,{atom,fp},{atom,dot1},6}.
{label,2}.
{gc_bif,'*',{f,0},6,[{x,0},{x,3}],{x,0}}.
{gc_bif,'*',{f,0},6,[{x,1},{x,4}],{x,1}}.
{gc_bif,'+',{f,0},6,[{x,0},{x,1}],{x,0}}.
{gc_bif,'*',{f,0},6,[{x,2},{x,5}],{x,1}}.
{gc_bif,'+',{f,0},6,[{x,0},{x,1}],{x,0}}.
{'%live',1}.
return.
{function, dot2, 6, 4}.
{label,3}.
{func_info,{atom,fp},{atom,dot2},6}.
{label,4}.
{test,is_float,{f,3},[{x,0}]}.
{test,is_float,{f,3},[{x,1}]}.
{test,is_float,{f,3},[{x,2}]}.
{test,is_float,{f,3},[{x,3}]}.
{test,is_float,{f,3},[{x,4}]}.
{test,is_float,{f,3},[{x,5}]}.
{test_heap,{alloc,[{words,0},{floats,1}]},6}.
{fmove,{x,0},{fr,0}}.
{fmove,{x,3},{fr,1}}.
fclearerror.
{bif,fmul,{f,0},[{fr,0},{fr,1}],{fr,0}}.
{fmove,{x,1},{fr,2}}.
{fmove,{x,4},{fr,3}}.
{bif,fmul,{f,0},[{fr,2},{fr,3}],{fr,2}}.
{bif,fadd,{f,0},[{fr,0},{fr,2}],{fr,0}}.
{fmove,{x,2},{fr,4}}.
{fmove,{x,5},{fr,5}}.
{bif,fmul,{f,0},[{fr,4},{fr,5}],{fr,2}}.
{bif,fadd,{f,0},[{fr,0},{fr,2}],{fr,0}}.
{fcheckerror,{f,0}}.
{fmove,{fr,0},{x,0}}.
{'%live',1}.
return.
One paper discussing how to do this is:
http://user.it.uu.se/~tobiasl/publications/floats.pdf
I'm not sure how that paper relates to the current
scheme used in Erlang/OTP, but as far as I know, all
the people involved are on the list.
Best,
Thomas
____________________________________________________________________________________
No need to miss a message. Get email on-the-go
with Yahoo! Mail for Mobile. Get started.
http://mobile.yahoo.com/mail
More information about the erlang-questions
mailing list