Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/michaelnisi/ring-benchmark
The ring benchmark from “Programming Erlang”
https://github.com/michaelnisi/ring-benchmark
Last synced: about 1 month ago
JSON representation
The ring benchmark from “Programming Erlang”
- Host: GitHub
- URL: https://github.com/michaelnisi/ring-benchmark
- Owner: michaelnisi
- Created: 2013-07-06T19:44:37.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-12-06T15:09:34.000Z (about 11 years ago)
- Last Synced: 2024-10-13T18:45:40.134Z (2 months ago)
- Language: Erlang
- Homepage:
- Size: 111 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ring-benchmark
In his book *Programming Erlang*, Joe Armstrong demands:
> Write a ring benchmark. Create N processes in a ring. Send a message round the ring M times so that a total of N * M messages get sent. Time how long this takes for different values of N and M.
So, here goes:
```Erlang
-module(ring).
-export([send/2]).%% @doc Send M messages through a ring of N processes.
send(M, N) ->
statistics(runtime),
H = lists:foldl(
fun(Id, Pid) -> spawn_link(fun() -> loop(Id, Pid, M) end) end,
self(),
lists:seq(N, 2, -1)),
{_, Time} = statistics(runtime),
io:format("~p processes spawned in ~p ms~n", [N, Time]),
statistics(runtime),
H ! M,
loop(1, H, M).loop(Id, Pid, M) ->
receive
1 ->
{_, Time} = statistics(runtime),
io:format("~p messages sent in ~p ms~n", [M, Time]),
exit(self(), ok);
Index ->
Pid ! Index - 1,
loop(Id, Pid, M)
end.
```To try this in shell:
```
$ erl
> c(ring).
{ok,ring}
> ring:send(100000,10000).
10000 processes spawned in 50 ms
100000 messages sent in 170 ms
** exception exit: ok
3>
```