[erlang-questions] list comprehensions speed

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Tue Feb 25 15:30:13 CET 2014


On Tue, Feb 25, 2014 at 2:08 PM, Oleg <lego12239@REDACTED> wrote:

>   Hello.
>

Hi Oleg!


>
> I'm erlang newbie. Don't beat me, please :-), if this question is obvious.
>
> I have a list of key-value pairs:
>
> A=[{"name1", 1}, {"name2", 77}, {"name3", 33}, {"name4", 234}].
>
> What is faster:
>
>
The way you can figure this out is by following three basic rules:

1. use timer:tc/1 to measure the execution time.
2. Remember to do it in a module which is compiled. The shell interprets.
3. Remember to only measure what you want to measure!

https://gist.github.com/jlouis/9209794

has an example. Note that for a 4-element list, the executiion speed
doesn't really matter that much. For a 1000 element list, the speed of
`lists:keyfind/3` begins to show itself. The reason is that keyfind is a
BIF and is implemented in C.

Also, like motiejus wrote, you should probably run more than a single run
in order to figure out what the best number of rounds are. And remember to
call the garbage_collector in between to clean up a bit.


-- 
J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140225/25ab3925/attachment.htm>


More information about the erlang-questions mailing list