[erlang-questions] Re: List comprehension questions
Björn Gustavsson
bgustavsson@REDACTED
Sun Apr 3 08:30:06 CEST 2011
> On 1 Apr 2011, at 20:08, Richard Carlsson wrote:
>
>> On 2011-04-01 19:24, James Churchman wrote:
[...]
>>> Also keen to know the body recursive "cost", and why it avoids using
>>> list reverse in the case when a list is required to be returned :-)
>>
>> A body recursive translation is simpler, and is generally faster for short lists (the majority of list comprehensions runs on lists shorter than 100 elements, in my experience at least, and the break-even point for tail recursion plus reverse is somewhere above 100 elements).
>>
>> Even for quite long lists, a body recursive function is typically not much worse than a tail recursive solution with a reverse at the end, so there's no reason to change the behaviour and make it worse for short lists. Hence, the current implementation never produces a list in reverse order, and it never needs to reverse anything at the end.
There used to be a more noticeable difference between
body recursion and tail recursion before the R12B release,
because body recursion would usually use more memory.
In R12B and later releases, the tail-recursive and
body-recursive variants of a function generally use the
same amount of memory and executes roughly in the
same time. See:
http://www.erlang.org/doc/efficiency_guide/myths.html#id58884
>> If the compiler knows that the result (which is always a list) won't be used at all, it could generate code more similar to lists:foreach() instead, but I don't think that's done currently.
It's done. See:
http://www.erlang.org/doc/efficiency_guide/listHandling.html#id61285
--
Björn Gustavsson, Erlang/OTP, Ericsson AB
More information about the erlang-questions
mailing list