Last active
March 22, 2022 15:22
-
-
Save davisp/bff874cd21e13c2ebbf70ec97ccf050a 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(slow_tuples). | |
-export([ | |
run/0 | |
]). | |
run() -> | |
run(7500, 0.0008, {1647,841737,351137}). | |
run(Depth, BranchChance, Seed) -> | |
Tree = gen_trees(Depth, BranchChance, Seed), | |
erlang:garbage_collect(), | |
MPid = spawn_mem_sampler(self(), 500), | |
T1 = erlang:monotonic_time(), | |
stem_tree(Tree, sets:new()), | |
T2 = erlang:monotonic_time(), | |
Max = get_max_mem(MPid), | |
erlang:garbage_collect(), | |
unlink(MPid), | |
exit(MPid, kill), | |
{deltaT(T2, T1), Max}. | |
stem_tree([], Seen) -> | |
{sets:add_element(rand:uniform(), Seen), -1, []}; | |
stem_tree(Children, Seen0) -> | |
Seen1 = sets:add_element(rand:uniform(), Seen0), | |
lists:foldl(fun(Child, Acc) -> | |
{SeenAcc, LimitPosAcc, ChildAcc} = Acc, | |
{NewSeenAcc, _, _} = stem_tree(Child, SeenAcc), | |
{NewSeenAcc, LimitPosAcc, ChildAcc} | |
end, {Seen1, -1, []}, Children). | |
gen_trees(Depth, BranchChance, Seed) -> | |
rand:seed(exrop, Seed), | |
node(Depth, BranchChance). | |
node(0, _) -> | |
[]; | |
node(Depth, BranchChance) -> | |
case rand:uniform() < BranchChance of | |
true -> | |
[ | |
node(Depth - 1, BranchChance), | |
node(Depth - 1, BranchChance) | |
]; | |
false -> | |
[node(Depth - 1, BranchChance)] | |
end. | |
deltaT(T0, T1) -> | |
erlang:convert_time_unit(T0 - T1, native, second). | |
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). | |
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment