Skip to content

Instantly share code, notes, and snippets.

@sa2ajj
Created May 5, 2011 08:12
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 sa2ajj/956717 to your computer and use it in GitHub Desktop.
Save sa2ajj/956717 to your computer and use it in GitHub Desktop.
atom to string conversion benchmark
-module(msr_subst).
-export([doit/0, props/0]).
doit() ->
timer:start(),
Data = data(),
io:format("check_prop: ~p~n", [check_prop(Data)]),
io:format("check_dict: ~p~n", [check_dict(Data)]),
io:format("check_match: ~p~n", [check_match(Data)]),
io:format("check_match2: ~p~n", [check_match2(Data)]),
ok.
props() ->
[
{filename, "Filename"},
{maintainer, "Maintainer"},
{description, "Description"},
{package, "Package"},
{section, "Section"},
{'installed-size', "Installed-Size"},
{priority, "Priority"},
{suggests, "Suggests"},
{depends, "Depends"},
{version, "Version"},
{source, "Source"},
{architecture, "Architecture"}
].
check_prop(Data) ->
measure(fun check_prop/3, Data, props()).
check_dict(Data) ->
measure(fun check_dict/3, Data, dict:from_list(props())).
check_match(Data) ->
measure(fun check_match/3, Data, none).
check_match2(Data) ->
measure(fun check_match2/3, Data, none).
measure(Fun, Arg1, Arg2) ->
Start = erlang:now(),
many_runs(Fun, Arg1, Arg2, 1000000),
timer:now_diff(erlang:now(), Start).
many_runs(_, _, _, 0) ->
ok;
many_runs(Fun, Data1, Data2, N) ->
Fun(Data1, Data2, []),
many_runs(Fun, Data1, Data2, N-1).
check_match([], _, Acc) ->
lists:reverse(Acc);
check_match([{Key, Data}=Head | Rest], Blah, Acc) ->
check_match(Rest, Blah, [case Key of
filename ->
{"Filename", Data};
maintainer ->
{"Maintainer", Data};
description ->
{"Description", Data};
package ->
{"Package", Data};
section ->
{"Section", Data};
'installed-size' ->
{"Installed-Size", Data};
priority ->
{"Priority", Data};
suggests ->
{"Suggests", Data};
depends ->
{"Depends", Data};
version ->
{"Version", Data};
source ->
{"Source", Data};
architecture ->
{"Architecture", Data};
_ ->
Head
end | Acc]).
do_case(Key) ->
case Key of
filename ->
"Filename";
maintainer ->
"Maintainer";
description ->
"Description";
package ->
"Package";
section ->
"Section";
'installed-size' ->
"Installed-Size";
priority ->
"Priority";
suggests ->
"Suggests";
depends ->
"Depends";
version ->
"Version";
source ->
"Source";
architecture ->
"Architecture";
_ ->
Key
end.
check_match2([], _, Acc) ->
lists:reverse(Acc);
check_match2([{Key, Data} | Rest], Blah, Acc) ->
check_match2(Rest, Blah, [{do_case(Key), Data} | Acc]).
check_prop([], _, Acc) ->
lists:reverse(Acc);
check_prop([{Key, Data} | Rest], Props, Acc) ->
check_prop(Rest, Props, [{proplists:get_value(Key, Props, Key), Data} | Acc]).
check_dict([], _, Acc) ->
lists:reverse(Acc);
check_dict([{Key, Data} | Rest], Props, Acc) ->
check_dict(Rest, Props, [{dict:fetch(Key, Props), Data} | Acc]).
data() ->
[
{filename,<<"libgda4-dev_4.0.4-0maemo0_i386.deb">>},
{maintainer,[<<"Gustavo R. Montesino <grmontesino@ig.com.br>">>]},
{description, [
<<"Development files for GNOME Data Access library for GNOME2">>,
<<"GNOME Data Access is an attempt to provide uniform access to">>,
<<"different kinds of data sources (databases, information servers,">>,
<<"mail spools, etc).">>,
<<"It is a complete architecture that provides all you need to">>,
<<"access your data.">>,<<".">>,
<<"This library is made for GNOME2, but it doesn't require GNOME2 libraries.">>,
<<".">>,
<<"This package contains the header files and libraries which">>,
<<"you need for development.">>
]},
{package,<<"libgda4-dev">>},
{section,<<"libdevel">>},
{'installed-size',<<"2432">>},
{priority,<<"optional">>},
{suggests,[<<"libgda4-doc">>]},
{depends, [
<<"libgda4-4 (= 4.0.4-0maemo0)">>,
<<"libglib2.0-dev (>= 2.2.0)">>,
<<"libxslt1-dev">>
]},
{version,<<"4.0.4-0maemo0">>},
{source,<<"libgda4">>},
{architecture,<<"i386">>}
].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment