Create a gist now

Instantly share code, notes, and snippets.

Microbenchmark timings for lists:reverse/1. Cf. http://jsfiddle.net/4fr5pzpy/1/
%% 1> reverse_bench:run().
%% [{10,0.17},
%% {100,0.47},
%% {1000,8.77},
%% {10000,90.72},
%% {100000,10446.0},
%% {1000000,10362.46},
%% {10000000,126800.38},
%% {100000000,36015715.28}]
-module(reverse_bench).
-export([run/0]).
run() ->
Steps = [10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000],
Sequences = [{N, lists:seq(1, N)} || N <- Steps],
Timings = [{N, avg_reverse_timing(Seq, 100)} || {N, Seq} <- Sequences],
io:format("~p~n", [Timings]).
avg_reverse_timing(List, N) ->
Timings = repeat_tc(lists, reverse, [List], N, []),
lists:sum(Timings) / length(Timings).
repeat_tc(_, _, _, 0, Acc) ->
Acc;
repeat_tc(M, F, A, N, Acc) ->
{Timing, _} = timer:tc(M, F, A),
repeat_tc(M, F, A, N - 1, [Timing | Acc]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment