[erlang-questions] case or clause

Richard Carlsson richardc@REDACTED
Fri Jan 9 10:18:35 CET 2009

Sten Kvamme wrote:
> Ok, and if I'm using several clauses, is it like it is in Prolog a good
> idea to put the clauses that match most often at the top?

Like Robert said, the compiler may reorder tests "under the hood" for
better efficiency, as long as it does not change the observable
behaviour in any way, which should always be as if the clauses were
tried in strictly top-down order. However, the compiler knows nothing
about the statistical probability of the different clauses, so if you
know more, you can reduce the amount of unnecessary tests by putting
more probable clauses first. This is also a kind of microoptimization,
that I don't recommend spending any effort on unless you are writing
something like a heavily used abstract data type such as the gb_trees
and array modules. In most other cases, clarity of expression should
be your guiding principle: optimize for maintainability first, and
for efficiency when you know your bottlenecks.


More information about the erlang-questions mailing list