Skip to content

Instantly share code, notes, and snippets.

@shino
Created October 15, 2008 06:29
Show Gist options
  • Save shino/16871 to your computer and use it in GitHub Desktop.
Save shino/16871 to your computer and use it in GitHub Desktop.
simple counter (just loop)
-module(simple_counter1).
-export([count_to/1, increment/2, test/0]).
count_to(Target) ->
increment(0, Target).
increment(Target, Target) ->
Target;
increment(Count, Target) ->
increment(Count + 1, Target).
test() ->
p("bench", {result, benchmark_elements()}),
TenEighth = round(math:pow(10,8)),
lists:map(fun(Target) ->
p("bench", benchmark(fun () -> count_to(Target) end))
end,
lists:map(fun (E) -> round(math:pow(10,E)) end, lists:seq(4,7))
++ lists:seq(TenEighth, 8*TenEighth, TenEighth)
).
p(Label, Message) ->
io:format("~s: ~p~n", [Label, Message]).
benchmark(TargetFunction) ->
lists:foreach(fun statistics/1, benchmark_elements()),
Result = TargetFunction(),
Times = lists:map(
fun (Type) -> {_, T} = statistics(Type), T end,
benchmark_elements()
),
{Result, Times}.
benchmark_elements() ->
[runtime, wall_clock].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment