Skip to content

Instantly share code, notes, and snippets.

@apage43
Created October 5, 2011 14:23
Show Gist options
  • Save apage43/1264552 to your computer and use it in GitHub Desktop.
Save apage43/1264552 to your computer and use it in GitHub Desktop.
erlang timer
-module(ticktock).
-export([tick/1, tock/1, report/1]).
tick(T) ->
erlang:put({tick, T}, os:timestamp()).
tock(T) ->
erlang:put({total, T}, timer:now_diff(os:timestamp(), erlang:get({tick, T}))
+ case erlang:get({total, T}) of
undefined -> 0;
Sum -> Sum
end).
total(T) ->
case erlang:get({total, T}) of
undefined -> 0;
Time -> Time
end.
report(Ts) ->
Sorted = lists:reverse(lists:keysort(2, [{T, total(T)} || T <- Ts])),
[{_, Max} | _] = Sorted,
WithPercentage = [{Elapsed/Max * 100, T, Elapsed} || {T, Elapsed} <- Sorted],
io:format("~62p", [WithPercentage]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment