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

Per Melin per.melin@REDACTED
Mon May 18 18:40:57 CEST 2009


Dennis Byrne:
>> 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 take it you didn't benchmark them? expressive_and_efficient() is
twice as fast as efficient(). I leave it to someone else to try to
explain why.

Something else that may be worth mentioning is that with a longer list
(than the [1,2,3] you used) the difference between efficient() and
expressive() shrinks and soon is only about 20%. I missed this at
first because it doesn't happen if you compile with HiPE.



More information about the erlang-questions mailing list