optimization tricks ?

Francesco Cesarini cesarini@REDACTED
Mon May 15 11:32:01 CEST 2000


Erlang methodology:

1. First make it work
2. Then make it beautiful
3. Then [if you *really* *really* have to] make it fast [keeping it
beautiful and correct].

In 9 cases out of 10, you will realize you do not need step 3 as the
application runs fast enough. In the 10th case, you probably won't have
time for the third step.

//FC

Hakan Stenholm wrote:
> 
> I'm currently working on a strategy game in my spare time (i.e. not a
> large distributed real time system) and have spent some time wondering
> about how to write efficent (speed and memory vise) erlang code.
> I know about tail recursion and last call optimization (allthough
> exactly when this ocures seams a bit vauge) and that handling files as
> binaries (if posible) can be significantly faster. But I have been
> wondering about several things and as erlang was originaly inspired by
> prolog these optimization tricks spring to mind:
> 
> * in Prolog the order of the predicate (function) clauses affect the
> speed of selecting the apropriate clause (first match is used). e.g. :
> do_something_per_element([E|Rest]) ->
>         do_something(E),
>         do_something_per_element(Rest);
> do_something_per_element([]) -> ok.
> 
> would be faster becouse the first clause is allways the right one expect
> in one case (when it is done).
> Does this apply to Erlang as well ?
> 
> * if the functions clauses use pattern matching in their heads e.g. :
>     F1(case_a, D) -> ....
>     F1(case_b, D) -> ....
> 
>     F2(D, case_a) -> ....
>     F2(D, case_b) -> ....
> 
> Prolog would be faster for F1 as pattern matching would not be needed
> for more than the first argument i.e. keys should be first and placed in
> such an order as to reduce the amount of possibal matching function
> clauses.
> Does this apply to Erlang as well ?
> - - - - - - - - - - - - - - - - - - - -
> 
> Are there any other non-algoritmic tricks for better perfomance ?
> Is pattern matching with atoms faster than strings (i.e. list of
> integers which will presumebly need to be matched letter for letter) ?
> 
> And yes I _DO_ know that the choice of algoritms is generaly far more
> important than tricks that can change the Ordo by some constant    : )
> 
> Håkan Stenholm

-- 
Francesco Cesarini

Erlang/OTP consultant
Cellular: +44-(0)7776 250381
ECN: 832-707192



More information about the erlang-questions mailing list