Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure time a function takes to execute from an erlang shell?

Tags:

erlang

Is there is a simple way to measure a function's execution time if I run that function from an erlang shell?

like image 470
Konstantin Avatar asked Jan 08 '10 23:01

Konstantin


People also ask

How do you find the time taken to execute a program in R?

To measure execution time of R code, we can use Sys. time function. Put it before and after the code and take difference of it to get the execution time of code.


2 Answers

Please see the article Measuring Function Execution Time.

It is all based on timer:tc/3 for the measurement.

like image 135
Christian Avatar answered Sep 18 '22 13:09

Christian


If you want to measure an anonymous function:

1> TC = fun(F) -> B = now(), V = F(), A = now(), {timer:now_diff(A,B), V} end.

2> F = fun() -> lists:seq(1,1000) end.
3> TC(F).
{47000,
 [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,
  23,24,25,26,27|...]}

average of N runs:

4> TCN2 = fun(T,F,0) -> ok; (T,F,N) -> F(), T(T,F,N-1) end.
5> TCN = fun(F,N) -> B=now(), TCN2(TCN2,F,N), A=now(), timer:now_diff(A,B)/N end.

6> TCN(F, 1000).
63.0
like image 28
Zed Avatar answered Sep 19 '22 13:09

Zed