Skip to content

Instantly share code, notes, and snippets.

@anujpradhaan
Forked from kidrane/pvsl.erl
Created June 17, 2016 07:15
Show Gist options
  • Save anujpradhaan/ad6b454c1dbf70e58cf5bb72dc7d7c64 to your computer and use it in GitHub Desktop.
Save anujpradhaan/ad6b454c1dbf70e58cf5bb72dc7d7c64 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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment