Created
August 22, 2015 19:21
-
-
Save User4574/b5c2ae4988c9d2554ea1 to your computer and use it in GitHub Desktop.
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(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