Erlang Efficiency quesitons

Robert Virding rv@REDACTED
Wed Mar 14 23:24:45 CET 2001


Francesco Cesarini <cesarini@REDACTED> writes:
>
>Let us put one together. Here are some more hints:
>
>* Use binaries when shuffling around large amounts of data between
>processes.
>* Some one on the list had mentioned that appending binaries results in
>them being copied, thus avoid it.
>* In the documentation for the 4.4 extensions I found a note that 
>  f(X) when X == #rec{x=1, y=a} -> ... is less efficient than
> f(#rec{x=1, y=a} = X) -> ... 
>
>I assume because in A X is bound before the guard test, in B, the
>pattern match fails as soon as one of the conditions does not match.
>Back in the good old days, the recommendation was to use guards in Beam,
>while with the Jam there was no difference... 

A is inefficient because you build a new record and then do an equals test on 
the two structures.  It is also EXTREMELY unsafe as you get the default values 
on all unmentioned fields!

B just test the type of term and values of the given fields.  It is infact 
more efficient to do:

f(#rec{x=X,a=A}=Rec) ->

than

f(Rec) when record(Rec, rec) ->
    ...
    Rec#rec.x  Rec#rec.a

and it is more efficient to just test for a rec record with the first case 
than with the second.

	Robert





More information about the erlang-questions mailing list