[erlang-questions] Intersection of two ordered sets

Richard Carlsson richardc@REDACTED
Tue May 12 10:18:01 CEST 2009

Andreas Hillqvist wrote:
> Hi.
> Sorry for hi-jacking this thread, but a question comes to my mind.
> Is there a performance diffrence between:
>      intersection([H1|T1], [H2|T2]) when H1 > H2 ->
> ...
> And:
>      intersection([H1|_] = L1, [H2|T2]) when H1 > H2 ->
>          intersection(L1, T2);
> ...
> Which one is the optimal, I'm leaning towords the second later
> ("[H1|_] = L1" ... "L1")?

Yes, the latter is more efficient. Note, though, that this kind of
optimization is only a valid thing to do in a heavily used library
function, when the allocation of a new cell would actually be a
significant part of the work done per element. In everyday code,
however, this sort of micro-optimization is mainly a waste of
programmer cpu time.

> If this is the case would it be a "simple task" for the compiler to
> transform these kind of statment?

It should not be very hard to do; it just seems that nobody has got
around to doing it yet.

> Would this be a valid EEP?

Not really; it doesn't affect the language per se.


More information about the erlang-questions mailing list