Skip to content

Instantly share code, notes, and snippets.

@davisp
Created March 21, 2022 21:44
Show Gist options
  • Save davisp/419810bd8738fe2023d2af98f1689238 to your computer and use it in GitHub Desktop.
Save davisp/419810bd8738fe2023d2af98f1689238 to your computer and use it in GitHub Desktop.
-module(slow_tuples).
-export([
run/0
]).
run() ->
Vals = gen(),
erlang:garbage_collect(),
MPid = spawn_mem_sampler(self(), 10),
T1 = erlang:monotonic_time(),
loop({Vals, 26, []}),
T2 = erlang:monotonic_time(),
Max = get_max_mem(MPid),
erlang:garbage_collect(),
unlink(MPid),
exit(MPid, kill),
io:format(standard_error, "~p~n", [{dt(T2, T1), Max}]).
gen() ->
gen(100000, sets:new()).
gen(0, Vals) ->
Vals;
gen(N, Vals) ->
Bin = <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, N:32>>,
gen(N - 1, sets:add_element(Bin, Vals)).
loop(Var) ->
{Vals, Count, Acc} = Var,
case loop1(Vals, Count - 1) of
ok ->
{ok, Vals, [Count | Acc]};
Else ->
{Else, Vals, [Count | Acc]}
end.
loop1(_Vals, 0) ->
ok;
loop1(Vals, Count) ->
loop({Vals, Count - 1, []}).
dt(T0, T1) ->
erlang:convert_time_unit(T0-T1, native, second).
mem_mb(Pid) ->
{memory, Words} = erlang:process_info(Pid, memory),
Bytes = Words * 8,
Bytes / (1024 * 1024).
get_max_mem(Pid) ->
Pid ! {get_mem, self()},
receive {mem_max, Max} -> Max end.
spawn_mem_sampler(Pid, DtMsec) ->
spawn_link(fun() -> mem_sampler(Pid, DtMsec, mem_mb(Pid)) end).
mem_sampler(Pid, DtMsec, Max0) ->
timer:sleep(DtMsec),
Max = max(mem_mb(Pid), Max0),
receive
{get_mem, From} ->
From ! {mem_max, Max}
after 0 ->
ok
end,
mem_sampler(Pid, DtMsec, Max).
% OTP 23
% couch_key_tree:gen_and_stem().
% {8,5790.3297119140625}
%
% OTP 20
% couch_key_tree:gen_and_stem().
% {3,617.4666748046875}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment