Skip to content

Instantly share code, notes, and snippets.

@unix1
Created August 10, 2014 16:44
Show Gist options
  • Save unix1/9794059532b033f3283c to your computer and use it in GitHub Desktop.
Save unix1/9794059532b033f3283c to your computer and use it in GitHub Desktop.
Measure average execution time of a function. I don't claim any credit, I took http://erlangcentral.org/wiki/index.php/Measuring_Function_Execution_Time and put it in a module for convenience.
-module(perftest).
-export([test_avg/4]).
test_avg(M, F, A, N) when N > 0 ->
L = test_loop(M, F, A, N, []),
Length = length(L),
Min = lists:min(L),
Max = lists:max(L),
Med = lists:nth(round((Length / 2)), lists:sort(L)),
Avg = round(lists:foldl(fun(X, Sum) -> X + Sum end, 0, L) / Length),
io:format("Range: ~b - ~b mics~n"
"Median: ~b mics~n"
"Average: ~b mics~n",
[Min, Max, Med, Avg]),
Med.
test_loop(_M, _F, _A, 0, List) ->
List;
test_loop(M, F, A, N, List) ->
{T, _Result} = timer:tc(M, F, A),
test_loop(M, F, A, N - 1, [T|List]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment