The Eight Myths of Erlang Performance

erlang有一篇guide很有意思: http://www.erlang.org/doc/efficiency_guide

里面有个The Eight Myths of Erlang Performance: http://www.erlang.org/doc/efficiency_guide/myths.html

 

Myth: Funs are slow

Myth: List comprehensions are slow

Myth: Tail-recursive functions are MUCH faster than recursive functions

Myth: '++' is always bad

Myth: Strings are slow

Myth: Repairing a Dets file is very slow

Myth: BEAM is a stack-based byte-code virtual machine (and therefore slow)

Myth: Use '_' to speed up your program when a variable is not used

 

另一篇文章http://www.erlang.org/doc/efficiency_guide/listHandling.html#id209936

里面提到了List comprehensions的实现,List comprehensions在erlang中被compiler转换成fun,如下:

[Expr(E) || E <- List]

将会被compile成下面的fun

'lc^0'([E|Tail], Expr) ->
    [Expr(E)|'lc^0'(Tail, Expr)];
'lc^0'([], _Expr) -> [].

 

里面有提到了另一种转化形式:

[io:put_chars(E) || E <- List]

'lc^0'([E|Tail], Expr) ->
    Expr(E),
    'lc^0'(Tail, Expr);
'lc^0'([], _Expr) -> [].
 因为此种情况下compiler可以知道List comprehensions的结果没有被使用,所以直接优化成尾递归。

 

你可能感兴趣的:(erlang)