问题
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?
回答1:
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).
回答2:
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).
回答3:
Take a look at timer:tc(Module, Function, Arguments)
来源:https://stackoverflow.com/questions/13381218/erlang-code-to-measure-time-operations-took-to-execute