Skip to content

Instantly share code, notes, and snippets.

Created November 30, 2012 15:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
The reftrick: code
%% 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)
plain() ->
?TIME(recv1, recv(10000)),
?TIME(recv2, recv(10000)).
ref() ->
?TIME(recv1, recv_ref(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_ref(0) -> ok;
recv_ref(N) -> Ref = make_ref(),
self() ! {Ref, bar},
receive {Ref, bar} -> ok end,
%%% eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment