Created
November 14, 2013 19:40
-
-
Save av-ast/7473015 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(benchmark). | |
-author('[email protected]'). | |
-export([run/1]). | |
run(Count) -> | |
Keys = lists:seq(1, Count), | |
lists:foreach( | |
fun ({F, TargetName}) -> | |
{[SetRunTime, SetWallClock], [GetRunTime, GetWallClock]} = F(Keys), | |
io:fwrite( | |
"--<~s>--~nset:~p(~p)ms~nget:~p(~p)ms~n", | |
[TargetName, SetRunTime, SetWallClock, GetRunTime, GetWallClock] | |
) | |
end, | |
[ | |
{fun pd/1, "process dictionary"}, | |
{fun dict/1, "dict"}, | |
{fun ets/1, "ets"}, | |
{fun gb_trees/1, "gb_trees"} | |
] | |
). | |
pd(Keys) -> | |
{_SetResult, SetTimes} = benchmark( | |
fun () -> lists:foreach(fun (N) -> put(N, a) end, Keys) end | |
), | |
{_GetResult, GetTimes} = benchmark( | |
fun () -> lists:map(fun (N) -> get(N) end, Keys) end | |
), | |
{SetTimes, GetTimes}. | |
dict(Keys) -> | |
{Dict, SetTimes} = benchmark( | |
fun () -> | |
lists:foldl(fun (N, D) -> D:store(N, a) end, dict:new(), Keys) | |
end | |
), | |
{_GetResult, GetTimes} = benchmark( | |
fun () -> | |
lists:map(fun (N) -> {ok, V} = Dict:find(N), V end, Keys) | |
end | |
), | |
{SetTimes, GetTimes}. | |
ets(Keys) -> | |
Ets = ets:new(test_ets, [bag, private]), | |
{_SetResult, SetTimes} = benchmark( | |
fun () -> | |
lists:foreach(fun (N) -> ets:insert(Ets, {N, a}) end, Keys) | |
end | |
), | |
{_GetResult, GetTimes} = benchmark( | |
fun () -> | |
lists:map(fun (N) -> [{_K, V}] = ets:lookup(Ets, N), V end, Keys) | |
end | |
), | |
{SetTimes, GetTimes}. | |
gb_trees(Keys) -> | |
{Tree, SetTimes} = benchmark( | |
fun () -> | |
lists:foldl( | |
fun (N, T) -> gb_trees:enter(N, a, T) end, | |
gb_trees:empty(), Keys | |
) | |
end | |
), | |
BalancedTree = gb_trees:balance(Tree), | |
{_GetResult, GetTimes} = benchmark( | |
fun () -> | |
lists:map(fun (N) -> gb_trees:get(N, BalancedTree) end, Keys) | |
end | |
), | |
{SetTimes, GetTimes}. | |
benchmark(TargetFunction) -> | |
lists:foreach(fun statistics/1, [runtime, wall_clock]), | |
Result = TargetFunction(), | |
Times = lists:map( | |
fun (Type) -> {_, T} = statistics(Type), T end, | |
[runtime, wall_clock] | |
), | |
{Result, Times}. | |
%%%% RESULTS %%%% | |
%4> benchmark:run(10000). | |
%--<process dictionary>-- | |
%set:10(3)ms | |
%get:0(0)ms | |
%--<dict>-- | |
%set:20(30)ms | |
%get:10(6)ms | |
%--<ets>-- | |
%set:10(7)ms | |
%get:0(3)ms | |
%--<gb_trees>-- | |
%set:70(75)ms | |
%get:10(6)ms | |
%5> benchmark:run(100000). | |
%--<process dictionary>-- | |
%set:30(39)ms | |
%get:10(24)ms | |
%--<dict>-- | |
%set:1290(1425)ms | |
%get:80(77)ms | |
%--<ets>-- | |
%set:90(104)ms | |
%get:70(72)ms | |
%--<gb_trees>-- | |
%set:1020(1090)ms | |
%get:90(90)ms | |
%6> benchmark:run(1000000). | |
%--<process dictionary>-- | |
%set:380(558)ms | |
%get:250(370)ms | |
%--<dict>-- | |
%set:78230(163962)ms | |
%get:1090(1158)ms | |
%--<ets>-- | |
%set:1470(1655)ms | |
%get:960(1055)ms | |
%--<gb_trees>-- | |
%set:12280(13397)ms | |
%get:950(1000)ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment