tags:

views:

150

answers:

2

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

+8  A: 

Please see the article Measuring Function Execution Time over at trapexit.org.

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

Christian
A: 

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
Zed