Created
November 30, 2012 15:28
-
-
Save 0xYUANTI/4176424 to your computer and use it in GitHub Desktop.
The reftrick: code
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(reftrick). | |
-compile(export_all). | |
%% Luke Gorrie's favourite profiling macro. | |
-define(TIME(Tag, Expr), | |
(fun() -> | |
%% NOTE: timer:tc/4 does an annoying 'catch' so we | |
%% need to wrap the result in 'ok' to be able to | |
%% detect an unhandled exception. | |
{__TIME, __RESULT} = | |
timer:tc(erlang, apply, [fun() -> {ok, Expr} end, []]), | |
io:format("time(~s): ~18.3fms ~999p~n", | |
[?MODULE, __TIME/1000, Tag]), | |
case __RESULT of | |
{ok, _} -> element(2, __RESULT); | |
{'EXIT', Error} -> exit(Error) | |
end | |
end)()). | |
plain() -> | |
?TIME(recv1, recv(10000)), | |
spam(10000), | |
?TIME(recv2, recv(10000)). | |
ref() -> | |
?TIME(recv1, recv_ref(10000)), | |
spam(10000), | |
?TIME(recv2, recv_ref(10000)). | |
spam(0) -> ok; | |
spam(N) -> self() ! foo, spam(N-1). | |
recv(0) -> ok; | |
recv(N) -> self() ! bar, | |
receive bar -> ok end, | |
recv(N-1). | |
recv_ref(0) -> ok; | |
recv_ref(N) -> Ref = make_ref(), | |
self() ! {Ref, bar}, | |
receive {Ref, bar} -> ok end, | |
recv_ref(N-1). | |
%%% eof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment