Skip to content

Instantly share code, notes, and snippets.

@kidrane
Created August 17, 2012 02:36
Show Gist options
  • Save kidrane/3375421 to your computer and use it in GitHub Desktop.
Save kidrane/3375421 to your computer and use it in GitHub Desktop.
benchmark between lists:keyfind and proplists:get_value
-module(pvsl).
-define(LIST_SIZES, [10000, 100000, 1000000]).
-define(RETRIES, 1000).
-compile(export_all).
start() ->
% test for different list sizes
lists:foreach(fun(N) -> test_list(N) end, ?LIST_SIZES).
test_list(ListSize) ->
% generate a list of size ListSize of {Key, Val} entries
KeyList = [{K, K} || K <- lists:seq(1, ListSize)],
% test this list against both functions
lists:foreach(fun(Type) -> get_val(Type, now(), KeyList, ListSize, ?RETRIES) end,
[proplists, lists]).
% test getting values, compute necessary time and output print results
get_val(Type, Start, _KeyList, ListSize, 0) ->
T = timer:now_diff(now(), Start),
io:format("computed ~p random key searches on a ~p-sized list in ~p ms using ~p~n",
[?RETRIES, ListSize, T/1000, Type]);
get_val(proplists, Start, KeyList, ListSize, Tries) ->
proplists:get_value(random:uniform(ListSize), KeyList),
get_val(proplists, Start, KeyList, ListSize, Tries - 1);
get_val(lists, Start, KeyList, ListSize, Tries) ->
lists:keyfind(random:uniform(ListSize), 1, KeyList),
get_val(lists, Start, KeyList, ListSize, Tries - 1).
@kidrane
Copy link
Author

kidrane commented Aug 17, 2012

@yowcow
Copy link

yowcow commented Aug 13, 2019

The link above is dead, so I'm just leaving the result for the code here.

% erl
Erlang/OTP 22 [erts-10.4.4] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Eshell V10.4.4  (abort with ^G)
1> c("pvsl").
pvsl.erl:4: Warning: export_all flag enabled - all functions will be exported
pvsl.erl:14: Warning: erlang:now/0: Deprecated BIF. See the "Time and Time Correction in Erlang" chapter of the ERTS User's Guide for more information.
pvsl.erl:19: Warning: erlang:now/0: Deprecated BIF. See the "Time and Time Correction in Erlang" chapter of the ERTS User's Guide for more information.
pvsl.erl:23: Warning: random:uniform/1: the 'random' module is deprecated; use the 'rand' module instead
pvsl.erl:26: Warning: random:uniform/1: the 'random' module is deprecated; use the 'rand' module instead
{ok,pvsl}
2> pvsl:start().
computed 1000 random key searches on a 10000-sized list in 66.823 ms using proplists
computed 1000 random key searches on a 10000-sized list in 16.104 ms using lists
computed 1000 random key searches on a 100000-sized list in 566.504 ms using proplists
computed 1000 random key searches on a 100000-sized list in 162.177 ms using lists
computed 1000 random key searches on a 1000000-sized list in 6362.325 ms using proplists
computed 1000 random key searches on a 1000000-sized list in 2230.582 ms using lists
ok
3>

@imodernleaf
Copy link

The link above is dead, so I'm just leaving the result for the code here.

% erl
Erlang/OTP 22 [erts-10.4.4] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Eshell V10.4.4  (abort with ^G)
1> c("pvsl").
pvsl.erl:4: Warning: export_all flag enabled - all functions will be exported
pvsl.erl:14: Warning: erlang:now/0: Deprecated BIF. See the "Time and Time Correction in Erlang" chapter of the ERTS User's Guide for more information.
pvsl.erl:19: Warning: erlang:now/0: Deprecated BIF. See the "Time and Time Correction in Erlang" chapter of the ERTS User's Guide for more information.
pvsl.erl:23: Warning: random:uniform/1: the 'random' module is deprecated; use the 'rand' module instead
pvsl.erl:26: Warning: random:uniform/1: the 'random' module is deprecated; use the 'rand' module instead
{ok,pvsl}
2> pvsl:start().
computed 1000 random key searches on a 10000-sized list in 66.823 ms using proplists
computed 1000 random key searches on a 10000-sized list in 16.104 ms using lists
computed 1000 random key searches on a 100000-sized list in 566.504 ms using proplists
computed 1000 random key searches on a 100000-sized list in 162.177 ms using lists
computed 1000 random key searches on a 1000000-sized list in 6362.325 ms using proplists
computed 1000 random key searches on a 1000000-sized list in 2230.582 ms using lists
ok
3>

👍👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment