public
anonymous / gist:50240
Created

  • Download Gist
gistfile1.hrl
Erlang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
-module(bm).
 
-export([funs/4]).
 
-define(INDENT, 10).
 
funs(Module, Prefix, Args, N) when is_atom(Prefix) ->
Funs = [Fun || {Fun, Arity} <- Module:module_info(exports),
is_matched(Fun, Prefix),
Arity =:= length(Args)],
funs(Module, Funs, Args, N);
funs(Module, Funs, Args, N) when is_list(Funs) ->
Results = lists:foldl(fun(Fun, Results) ->
clear_line(),
io:format("~p...", [Fun]),
[{Fun, tc(Module, Fun, Args, N)} | Results]
end, [], Funs),
clear_line(),
 
print_results(lists:keysort(2, Results)).
 
is_matched(Fun, Prefix) ->
lists:prefix(atom_to_list(Prefix), atom_to_list(Fun)).
 
clear_line() ->
Columns = element(2, io:columns()) - 1,
io:format("\r~s\r", [lists:duplicate(Columns, $\s)]).
 
tc(Module, Fun, Args, N) ->
statistics(runtime),
statistics(wall_clock),
repeat(Module, Fun, Args, N),
{_, Runtime} = statistics(runtime),
{_, WallClock} = statistics(wall_clock),
 
{Runtime, WallClock}.
 
repeat(_, _, _, 0) ->
ok;
repeat(Module, Fun, Args, N) ->
apply(Module, Fun, Args),
repeat(Module, Fun, Args, N - 1).
 
print_results(Results) ->
FieldWidth = field_with(Results),
lists:foreach(fun({Fun, {Runtime, WallClock}}) ->
io:format("~*...w~p (~p) ms.~n", [-FieldWidth, Fun, Runtime, WallClock])
end, Results).
 
field_with(Results) ->
lists:foldl(fun({Fun, _}, MaxLen) ->
lists:max([MaxLen, length(atom_to_list(Fun))])
end, 0, Results) + ?INDENT.
gistfile2.hrl
Erlang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
-module(sample).
-compile(export_all).
 
bm() ->
bm:funs(?MODULE, decompress, [<<1,0,3,0,4,0,10,0,15,0,150>>], 500000).
 
bm2() ->
bm:funs(?MODULE, [decompress_1, decompress_2], [<<1,0,3,0,4,0,10,0,15,0,150>>], 500000).
 
decompress_1(Bin) when is_binary(Bin) ->
decompress_1(Bin, []).
decompress_1(<<0, Count, Tail/binary>>, Acc) ->
decompress_1(Tail, [<<0:(Count * 8)>> | Acc]);
decompress_1(<<Head, Tail/binary>>, Acc) ->
decompress_1(Tail, [Head | Acc]);
decompress_1(<<>>, Acc) ->
list_to_binary(lists:reverse(Acc)).
 
decompress_2(Bin) when is_binary(Bin) ->
decompress_2(Bin, <<>>).
decompress_2(<<0, Count, Tail/binary>>, Acc) ->
decompress_2(Tail, <<Acc/binary, 0:(Count * 8)>>);
decompress_2(<<Head, Tail/binary>>, Acc) ->
decompress_2(Tail, <<Acc/binary, Head>>);
decompress_2(<<>>, Acc) ->
Acc.
 
decompress_3(Bin) when is_binary(Bin) ->
decompress_3(Bin, []).
decompress_3(<<0, Count, Tail/binary>>, Acc) ->
decompress_3(Tail, [Acc, <<0:(Count * 8)>>]);
decompress_3(<<Head, Tail/binary>>, Acc) ->
decompress_3(Tail, [Acc, Head]);
decompress_3(<<>>, Acc) ->
list_to_binary(Acc).

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.