Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jlouis
Created February 25, 2014 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlouis/9209794 to your computer and use it in GitHub Desktop.
Save jlouis/9209794 to your computer and use it in GitHub Desktop.
-module(z).
-export([t0/0, t1/0]).
t0() ->
A = [{"name1", 1}, {"name2", 77}, {"name3", 33}, {"name4", 234}],
t(A).
t1() ->
A = [{"name1", 1}, {"name2", 77}, {"name3", 33}, {"name4", 234}],
B = [{x, y} || _ <- lists:seq(0, 1000)],
t(B ++ A).
t(A) ->
garbage_collect(),
{T0, R} = timer:tc(fun () -> lc(A) end),
garbage_collect(),
{T1, R} = timer:tc(fun () -> get_value("name3", A) end),
garbage_collect(),
{T2, R} = timer:tc(fun() -> {_, V} = lists:keyfind("name3", 1, A), [V] end),
{T0, T1, T2}.
lc(A) ->
[ Value || {Name, Value} <- A, string:equal(Name, "name3")].
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.
%% Runtimes in μs
% 9> z:t0().
% {2,2,1}
% 10> z:t1().
% {65,67,20}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment