[erlang-questions] about tail-recursive

Richard Carlsson <>
Wed Apr 16 13:03:17 CEST 2008


Pierpaolo Bernardi wrote:
> On 16 Apr 2008 11:27:48 +0200, Bjorn Gustavsson <> wrote:
> 
>>  The tail-recursive version would not consume any stack, but it would have to
>>  build a list in reverse order, which it would have to reverse by calling lists:reverse/1.
>>  The stack space and the list consumes exactly the same amount of memory (in R12B).
>>
>>  See
>>
>>  http://www.erlang.org/doc/efficiency_guide/myths.html#2.3
> 
> BTW, the efficiency guide says:
> 
> "It depends. On Solaris/Sparc, the body-recursive function seems to be
> slightly faster, even for lists with very many elements. On the x86
> architecture, tail-recursion was up to about 30 percent faster."
> 
> In this case I found lists:map to be slightly faster than my tail recursive
> map on a pentium M760 (centrino, single core, 2Ghz, slow bus)
> 
> Any explanation?

Not as such, but how you test can make a difference. The tail recursive
version puts more pressure on the garbage collector, since the memory
used for the temporary list cannot be reused immediately by the next call
to map (which it can in the body-recursive version). So things like initial
heap size affects the measurements.

     /Richard





More information about the erlang-questions mailing list