Created
October 5, 2011 14:23
-
-
Save apage43/1264552 to your computer and use it in GitHub Desktop.
erlang timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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