[erlang-questions] continuation vs. accumulator

Matthew Dempsky <>
Mon Jul 30 08:40:13 CEST 2007


On 7/29/07, June Kim <> wrote:
> Why is continuation passing style faster than accumulator passing
> style in erlang?

It's not.  Your CPS implementation evaluates (...(((1 * 2) * 3) * 4) *
...) while your APS implementation evaluates 1 * (2 * (3 * (4 *
...))).  Your APS implementation handles larger integers during its
execution, which accounts for the slow down.

On my laptop, the following code outperforms all three of your implementations.

fact_aps2(N) -> fact_aps2(N,1,1).
fact_aps2(N,M,Acc) ->
    Acc2 = M * Acc,
    if N == M -> Acc2;
       true -> fact_aps2(N,M + 1,Acc2)
    end.



More information about the erlang-questions mailing list