[erlang-questions] list comprehensions speed

Ivan Uemlianin <>
Tue Feb 25 15:29:32 CET 2014


Sorry, typo:

 > I think the functions in the lists module are written in C, so they 
should be fast.

On 25/02/2014 14:28, Ivan Uemlianin wrote:
> There's also lists:keyfind/3 you could try
>
>    http://www.erlang.org/doc/man/lists.html#keyfind-3
>
> I think the lists are written in C, so they should be fast.
>
> Ivan
>
>
> On 25/02/2014 13:08, Oleg wrote:
>>    Hello.
>>
>> 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:
>>
>> [ Value || {Name, Value} <- A, string:equal(Name, "name3")].
>>
>> Or:
>>
>> get_value(Key, []) ->
>>    [].
>> get_value(Key, [H|T]) ->
>>    {Name, Value} = H,
>>    case string:equal(Name, Key) of
>>      true ->
>>        [Value];
>>      false ->
>>        get_value(Key, T)
>>    end.
>>
>> start() ->
>>    get_value("name3", A).
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>

-- 
============================================================
Ivan A. Uemlianin PhD
Llaisdy
Speech Technology Research and Development

                     
                      www.llaisdy.com
                          llaisdy.wordpress.com
               github.com/llaisdy
                      www.linkedin.com/in/ivanuemlianin

                         festina lente
============================================================



More information about the erlang-questions mailing list