Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang code to measure time operations took to execute?

Tags:

erlang

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?

like image 394
mezamorphic Avatar asked Nov 14 '12 14:11

mezamorphic


2 Answers

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).

like image 27
Thanos Tintinidis Avatar answered Sep 18 '22 00:09

Thanos Tintinidis


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).

like image 173
atomh33ls Avatar answered Sep 22 '22 00:09

atomh33ls