Last active
November 21, 2024 11:43
-
-
Save RoadRunnr/1c6786029ac4e9d13ed30ed978850bbf to your computer and use it in GitHub Desktop.
Erlang maps benchmark
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
-module(t7). | |
-export([test/2, test/3]). | |
test(Prefix, Tries) -> | |
T = [1, 2, 4, 8, 16, 24, 32, 33, 64, 128], | |
Res = [test_3(Prefix, Items, Tries) || Items <- T], | |
io:format("| Tries | Atom | Binary | List |\n" | |
"|-------|--------------------------------|--------------------------------|--------------------------------|\n"), | |
[io:format("| ~5w | ~10w µs, ~f µs/try | ~10w µs, ~f µs/try | ~10w µs, ~f µs/try |~n", | |
[Items, L2, L2/Tries, L1, L1/Tries, L3, L3/Tries]) | |
|| {Items, L1, L2, L3} <- Res], | |
ok. | |
test(Prefix, Items, Tries) -> | |
{_, L1, L2, L3} = test_3(Prefix, Items, Tries), | |
io:format("Bin: ~w µs, ~f µs/try~n", [L1, L1/Tries]), | |
io:format("Atom: ~w µs, ~f µs/try~n", [L2, L2/Tries]), | |
io:format("List: ~w µs, ~f µs/try~n", [L3, L3/Tries]), | |
ok. | |
test_3(Prefix, Items, Tries) -> | |
KeysBin = [<<Prefix/binary, $-, (integer_to_binary(X))/binary>> | |
|| X <- lists:seq(0, Items-1)], | |
KeysAtom = [binary_to_atom(X) || X <- KeysBin], | |
KeysList = [binary_to_list(X) || X <- KeysBin], | |
io:format("Init Done (Prefix: ~p, Items: ~p, Tries: ~p)....~n", [Prefix, Items, Tries]), | |
L1 = test_2(Tries, KeysBin), | |
L2 = test_2(Tries, KeysAtom), | |
L3 = test_2(Tries, KeysList), | |
{Items, L1, L2, L3}. | |
force_copy(B) when is_binary(B) -> binary:copy(B); | |
force_copy(A) when is_atom(A) -> binary_to_atom(force_copy(atom_to_binary(A))); | |
force_copy(L) when is_list(L) -> binary_to_list(force_copy(list_to_binary(L))). | |
make_rnd(0, _Len, _Keys, _Seed) -> | |
[]; | |
make_rnd(Cnt, Len, Keys, Seed0) -> | |
{Pos, Seed} = rand:uniform_s(Len, Seed0), | |
Key = force_copy(array:get(Pos - 1, Keys)), | |
[Key | make_rnd(Cnt - 1, Len, Keys, Seed)]. | |
do_test([], _) -> | |
ok; | |
do_test([Key | More], Items) -> | |
_ = map_get(Key, Items), | |
do_test(More, Items). | |
%% do_test(More, Items#{Key := Key}). | |
test_2(Tries, Keys) -> | |
Map = maps:from_keys(Keys, []), | |
KeyArr = array:from_list(Keys), | |
Sel = make_rnd(Tries, array:size(KeyArr), KeyArr, rand:seed(default, 0)), | |
io:format("generation done, running test...~n"), | |
{Time, _} = timer:tc(fun() -> do_test(Sel, Map), ok end), | |
Time. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment