Guest User

Untitled

a guest
May 24th, 2018
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. % Write a ring benchmark. Create N processes in a ring. Send a message
  2. % round the ring M times so that a total of N * M messages get sent.
  3. % Time how long this takes for different values of N and M.
  4. % -- Joe Armstrong, "Programming Erlang"
  5.  
  6. % Copyright (c) 2008-2010 Ivan Fomichev
  7. %
  8. % Permission is hereby granted, free of charge, to any person obtaining a copy
  9. % of this software and associated documentation files (the "Software"), to deal
  10. % in the Software without restriction, including without limitation the rights
  11. % to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. % copies of the Software, and to permit persons to whom the Software is
  13. % furnished to do so, subject to the following conditions:
  14. %
  15. % The above copyright notice and this permission notice shall be included in
  16. % all copies or substantial portions of the Software.
  17. %
  18. % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. % IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. % FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. % AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. % LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. % OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. % THE SOFTWARE.
  25.  
  26. % SYNOPSIS
  27. %
  28. % R = ring:new(5, verbose), R ! {self(), {message, 3, hello}}.
  29. %
  30. % Start = now(),
  31. % R2 = ring:new(10000),
  32. % R2 ! {self(), {message, 10000000, hello}},
  33. % End = now(),
  34. % timer:now_diff(End, Start) / 1000000.
  35.  
  36. -module(ring).
  37. -export([new/1,new/2]).
  38.  
  39. report(verbose, Format, Params) ->
  40. io:format(Format, Params);
  41.  
  42. report(quiet, _, _) -> ok.
  43.  
  44. new(N) -> new(N, quiet).
  45.  
  46. new(N, Verbose) when N >= 1 ->
  47. First = spawn(fun() -> loop(none, Verbose) end),
  48. report(Verbose, "~p says: new ~p~n", [self(), First]),
  49. Last = new(2, N, First, Verbose),
  50. First ! {self(), {sends_to, Last}},
  51. First.
  52.  
  53. new(I, N, SendsTo, Verbose) when I =< N ->
  54. Pid = spawn(fun() -> loop(SendsTo, Verbose) end),
  55. report(Verbose, "~p says: new ~p sends messages to ~p~n", [self(), Pid, SendsTo]),
  56. new(I+1, N, Pid, Verbose);
  57.  
  58. new(_, _, Pid, _) -> Pid.
  59.  
  60. loop(SendsTo, Verbose) ->
  61. Self = self(),
  62. receive
  63. {_, {sends_to, Pid}} ->
  64. report(Verbose, "~p says: ~p sends messages to ~p~n", [Self, Self, Pid]),
  65. loop(Pid, Verbose);
  66. {From, {message, M, Message}} ->
  67. report(Verbose, "~p got ~p from ~p, more ~p laps~n", [Self, Message, From, M]),
  68. SendsTo ! {Self, {pass, Self, M, Message}},
  69. loop(SendsTo, Verbose);
  70. {From, {pass, Self, 1, Message}} ->
  71. report(Verbose, "~p got ~p from ~p, quit~n", [Self, Message, From]),
  72. loop(SendsTo, Verbose);
  73. {From, {pass, Self, M, Message}} ->
  74. report(Verbose, "~p got ~p from ~p, more ~p laps~n", [Self, Message, From, M-1]),
  75. SendsTo ! {Self, {pass, Self, M-1, Message}},
  76. loop(SendsTo, Verbose);
  77. {From, {pass, Origin, M, Message}} ->
  78. report(Verbose, "~p got ~p from ~p~n", [Self, Message, From]),
  79. SendsTo ! {Self, {pass, Origin, M, Message}},
  80. loop(SendsTo, Verbose)
  81. end.
Add Comment
Please, Sign In to add comment