Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.