[erlang-questions] At what point am I "playing compiler"

Dennis Byrne dennisbyrne@REDACTED
Mon May 18 16:46:08 CEST 2009


> How about:
>
> expressive_and_efficient() ->
>    List = [1,2,3],
>    {lists:min(List), lists:max(List), lists:sum(List)}.
>
> :-)
>

My understanding is that lists:min, lists:max, and lists:sum are all
O(N).  If this is the case, won't this function will run 3x times my
efficient function (after I replace 'Last = lists:last' with
'AnyElement = lists:nth'.

> I run micro-benchmarks obsessively. On my machine expressive() is 70%
> slower than efficient() without HIPE and 150% slower with HIPE. But
> we're talking fractions of a microsecond per execution. Should you
> bother?
>
> If you're concerned with efficiency, timer:tc/3 is your friend. Stuff
> like this you should obviously loop at least a few million times to
> get reliable times.
>
-- 
Dennis Byrne



More information about the erlang-questions mailing list