Is there is a simple way to measure a function's execution time if I run that function from an erlang shell?
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.
Please see the article Measuring Function Execution Time.
It is all based on timer:tc/3 for the measurement.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With