Skip to content

Instantly share code, notes, and snippets.

@emauton
Created January 24, 2015 21:05
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 emauton/aee7dd9bcac3036b4b1c to your computer and use it in GitHub Desktop.
Save emauton/aee7dd9bcac3036b4b1c to your computer and use it in GitHub Desktop.
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