Created
February 6, 2017 22:29
-
-
Save jadeallenx/ecdd65a3326d6b34950926e55fa508ca to your computer and use it in GitHub Desktop.
Comparing Erlang set implementations
This file contains hidden or 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
%% Comparing sets implementations | |
%% | |
%% Uses this library for term size: https://github.com/okeuday/erlang_term | |
%% | |
%% Compile / run: | |
%% git clone https://github.com/okeuday/erlang_term | |
%% cd erlang_term | |
%% (copy this file into the directory and...) | |
%% rebar3 shell | |
%% 1> c(big_set_merge). | |
%% 2> big_set_merge:test_all(). | |
%% 3> big_set_merge:really_big_test(). | |
-module(big_set_merge). | |
-compile([export_all]). | |
gen_num() -> | |
rand:uniform(255). | |
generate_ip_tuple() -> | |
{gen_num(), gen_num(), gen_num(), gen_num()}. | |
make_ordset(N) -> make_set(ordsets, ordsets:new(), N). | |
make_set(M, S, N) -> | |
case M:size(S) of | |
X when X > N -> S; | |
_ -> make_set(M, M:add_element(generate_ip_tuple(), S), N) | |
end. | |
make_set(M, N) -> make_set(M, M:new(), N). | |
time_this(F) -> | |
{Time, _Value} = timer:tc(F), | |
Time / 1000. | |
test(M) -> | |
io:format("Making small ~s...", [M]), | |
{T1, Small1} = timer:tc(fun() -> make_set(M, 1000) end), | |
{T2, Small2} = timer:tc(fun() -> make_set(M, 1000) end), | |
io:format("Done in ~p milliseconds~n", [(T1+T2) / 1000]), | |
io:format("Small1 size: ~p ", [erlang_term:byte_size(Small1)/1024]), | |
io:format("Small2 size: ~p~n", [erlang_term:byte_size(Small2)/1024]), | |
io:format("Making medium ~s...", [M]), | |
{T3, Medium1} = timer:tc(fun() -> make_set(M, 10000) end), | |
{T4, Medium2} = timer:tc(fun() -> make_set(M, 10000) end), | |
io:format("Done in ~p milliseconds~n", [(T3+T4) / 1000]), | |
io:format("Medium1 size: ~p ", [erlang_term:byte_size(Medium1)/1024]), | |
io:format("Medium2 size: ~p~n", [erlang_term:byte_size(Medium2)/1024]), | |
io:format("Making big ~s...", [M]), | |
{T5, Big1} = timer:tc(fun() -> make_set(M, 100000) end), | |
{T6, Big2} = timer:tc(fun() -> make_set(M, 100000) end), | |
io:format("Big1 size: ~p ", [erlang_term:byte_size(Big1)/1024]), | |
io:format("Big2 size: ~p~n", [erlang_term:byte_size(Big2)/1024]), | |
io:format("Done in ~p milliseconds~n", [(T5+T6) / 1000]), | |
{M, | |
[ | |
{small, time_this(fun() -> M:union(Small1, Small2) end)}, | |
{medium, time_this(fun() -> M:union(Medium1, Medium2) end)}, | |
{big, time_this(fun() -> M:union(Big1, Big2) end)} | |
] | |
}. | |
test_all() -> | |
[test(gb_sets), test(sets), test(ordsets)]. | |
really_big_test() -> | |
[really_big(gb_sets), really_big(sets)]. | |
really_big(M) -> | |
io:format("Making really big ~s...", [M]), | |
{T1, Big1} = timer:tc(fun() -> make_set(M, 1000000) end), | |
{T2, Big2} = timer:tc(fun() -> make_set(M, 1000000) end), | |
io:format("Done in ~p milliseconds~n", [(T1+T2) / 1000]), | |
io:format("ReallyBig1 size: ~p ", [erlang_term:byte_size(Big1)/1024]), | |
io:format("ReallyBig2 size: ~p~n", [erlang_term:byte_size(Big2)/1024]), | |
{M, [{really_big, time_this(fun() -> M:union(Big1, Big2) end)}]}. |
This file contains hidden or 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
2> big_set_merge:test_all(). | |
Making small gb_sets...Done in 14.869 milliseconds | |
Small1 size: 70.4140625 Small2 size: 70.4140625 | |
Making medium gb_sets...Done in 76.355 milliseconds | |
Medium1 size: 703.2265625 Medium2 size: 703.2265625 | |
Making big gb_sets...Big1 size: 7031.3515625 Big2 size: 7031.3515625 | |
Done in 838.458 milliseconds | |
Making small sets...Done in 3.348 milliseconds | |
Small1 size: 57.21875 Small2 size: 57.21875 | |
Making medium sets...Done in 42.277 milliseconds | |
Medium1 size: 565.15625 Medium2 size: 565.15625 | |
Making big sets...Big1 size: 5757.03125 Big2 size: 5757.03125 | |
Done in 719.36 milliseconds | |
Making small ordsets...Done in 14.997 milliseconds | |
Small1 size: 54.75 Small2 size: 54.75 | |
Making medium ordsets...Done in 1350.865 milliseconds | |
Medium1 size: 546.9375 Medium2 size: 546.9375 | |
Making big ordsets...Big1 size: 5468.8125 Big2 size: 5468.8125 | |
Done in 153109.07 milliseconds | |
[{gb_sets,[{small,0.264},{medium,1.985},{big,71.728}]}, | |
{sets,[{small,0.714},{medium,10.88},{big,479.43}]}, | |
{ordsets,[{small,0.109},{medium,0.765},{big,7.69}]}] | |
3> big_set_merge:really_big_test(). | |
Making really big gb_sets...Done in 11796.4 milliseconds | |
ReallyBig1 size: 70312.6015625 ReallyBig2 size: 70312.6015625 | |
Making really big sets...Done in 112095.519 milliseconds | |
ReallyBig1 size: 56991.78125 ReallyBig2 size: 56991.78125 | |
[{gb_sets,[{really_big,649.875}]}, | |
{sets,[{really_big,162661.868}]}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment