Could somebody be so kind as to point me towards some erlang code which allows me to time how long it takes to run certain pieces of code?
I havent seen an erlang library where this is available?
There is the timer library; check tc/[1-3].
You can also use erlang:now/0 to collect timestamps and then calculate the duration (now_diff/2 is really useful for that).
You can use the erlang:statistics
function.
This is used in Joe Armstrong's Programming Erlang book (p141).
e.g.
yourfun() ->
statistics(runtime),
statistics(wall_clock),
% your code here
{_, Time1} = statistics(runtime),
{_, Time2} = statistics(wall_clock),
U1 = Time1 * 1000,
U2 = Time2 * 1000,
io:format("Code time=~p (~p) microseconds~n",
[U1,U2]).
In this example U1
is the CPU time and U2
is the total elapsed time (wall clock time).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With