Skip to content

Instantly share code, notes, and snippets.

@User4574
Created August 22, 2015 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save User4574/b5c2ae4988c9d2554ea1 to your computer and use it in GitHub Desktop.
Save User4574/b5c2ae4988c9d2554ea1 to your computer and use it in GitHub Desktop.
-module(ring).
-export([bench/2, ring/2]).
bench(N, M) ->
timer:tc(?MODULE, ring, [N, M]).
ring(N, M) ->
SPID = spawn(fun() -> createstartnode() end),
LPID = create_ring(N-1, SPID),
SPID ! LPID,
SPID ! (M+1),
ok.
create_ring(0, PID) ->
PID;
create_ring(N, PID) ->
NPID = spawn(fun() -> anode(PID) end),
create_ring(N-1, NPID).
createstartnode() ->
receive
PID when is_pid(PID) ->
startnode(PID)
end.
startnode(PID) ->
receive
0 ->
PID ! 0;
N when is_integer(N) ->
PID ! (N-1),
startnode(PID)
end.
anode(PID) ->
receive
0 ->
PID ! 0;
N when is_integer(N) ->
PID ! N,
anode(PID)
end.
% $ erl
% Erlang R16B03 (erts-5.10.4) [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false]
%
% Eshell V5.10.4 (abort with ^G)
% 1> c(ring).
% {ok,ring}
% 2> ring:bench(50000, 100000).
% {145984,ok}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment