just start with code:
-module(test).
-export([run/1, run_1/1]).
run(N) ->
Self = self(),
First = timer:tc(fun run_1/1, [N]),
Second = timer:tc(fun run_1/1, [N]),
erlang:garbage_collect(),
spawn(fun() -> Self ! {run_1, timer:tc(fun run_1/1, [N])} end),
receive
{run_1, Three} ->
{First, Second, Three}
end.
run_1(0)->
erlang:process_info(self(), memory);
run_1(N) ->
try
do_somthing,
run_1(N - 1)
catch
_:_ ->
error
end.
runing on erl shell:
Erlang/OTP 25 [erts-13.0] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]
Eshell V13.0 (abort with ^G)
1> test:run(10000000).
{{2272172,{memory,177426856}},
{112110,{memory,177426856}},
{1733515,{memory,177423808}}}
2> test:run(10000000).
{{2255622,{memory,177426856}},
{103118,{memory,177426856}},
{1629922,{memory,177423808}}}
3> timer:tc(fun test:run_1/1, [10000000]).
{2251741,{memory,177436632}}
4> timer:tc(fun test:run_1/1, [10000000]).
{90485,{memory,177436632}}
5> timer:tc(fun test:run_1/1, [10000000]).
{130807,{memory,177436632}}
the question is
- Why it cost so many memory and time on the first
run_1/1
- In spawn seems have a speed up after first execute
- Continuous execution
run_1/1
only slow in the first times
of course, this try...catch recursion
just for learning