Skip to content

Instantly share code, notes, and snippets.

@elbrujohalcon
Created July 1, 2020 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elbrujohalcon/d4e995fbc4b93fadddfd1f0d6b9f8121 to your computer and use it in GitHub Desktop.
Save elbrujohalcon/d4e995fbc4b93fadddfd1f0d6b9f8121 to your computer and use it in GitHub Desktop.
OTP22 vs OTP21 Benchmarking
%% @doc Benchmark functions over lists.
%% Use it like: c(test), test:bench({test, rec}, 250, 5000, 2000).
-module test.
-export [
bench/4,
bench_tc/2,
rec/1,
lrec/1
].
bench(Function, Tests, ListCount, Length) ->
Lists = lists:duplicate(ListCount, lists:seq(1, Length)),
%% Drop 2 outliers
[_|TimesPlusOne] = lists:sort([bench_tc(Function, Lists) || _ <- lists:seq(1, Tests + 2)]),
[_|Times] = lists:reverse(TimesPlusOne),
%% Compute the Average
lists:sum(Times) / Tests.
-spec bench_tc(flatmap | {module(), function()}, [[any()]]) -> number().
bench_tc({Module, Function}, Lists) ->
element(1, timer:tc(Module, Function, [Lists]));
bench_tc(flatmap, Lists) ->
element(1, timer:tc(lists, flatmap, [fun(X) -> X end, Lists])).
rec([Hd|Tail]) -> Hd ++ rec(Tail);
rec([]) -> [].
lrec([Hd|Tail]) -> length(Hd) + lrec(Tail);
lrec([]) -> 0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment