Erlang code to measure time operations took to execute?

女生的网名这么多〃 提交于 2019-12-09 10:23:46

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!