Created
July 1, 2020 10:19
-
-
Save elbrujohalcon/d4e995fbc4b93fadddfd1f0d6b9f8121 to your computer and use it in GitHub Desktop.
OTP22 vs OTP21 Benchmarking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% @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