用于测量执行时间操作的Erlang代码?

mez*_*hic 8 erlang

有人可以如此友善地指出我的某些erlang代码,它允许我计算运行某些代码片段需要多长时间?

我还没有看到有可用的erlang库吗?

ato*_*3ls 18

你可以使用这个erlang:statistics功能.

这用于Joe Armstrong的编程Erlang书(p141).

例如

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]).
Run Code Online (Sandbox Code Playgroud)

在此示例中U1是CPU时间,U2是总耗用时间(挂钟时间).


Tha*_*dis 10

计时器库; 检查tc/[1-3].

您还可以使用erlang:now/0来收集时间戳,然后计算持续时间(now_diff/2对此非常有用).


小智 5

 Take a look at timer:tc(Module, Function, Arguments)
Run Code Online (Sandbox Code Playgroud)