<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Equars People  - Erlang ring benchmark Changes</title>
  <id>tag:people.equars.com,2008:/2008/5/22/erlang-ring-benchmark/changes</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://people.equars.com/2008/5/22/erlang-ring-benchmark/changes.xml" rel="self" type="application/atom+xml"/>
  <link href="/2008/5/22/erlang-ring-benchmark" rel="alternate" type="text/html"/>
  <updated>2008-05-22T15:30:03Z</updated>
  <entry xml:base="http://people.equars.com/">
    <author>
      <name>enrico</name>
    </author>
    <id>tag:people.equars.com,2008-05-22:187:10</id>
    <updated>2008-05-22T15:30:03Z</updated>
    <link href="http://people.equars.com/2008/12/1/erlang-ring-benchmark" rel="alternate" type="text/html"/>
    <title>Erlang ring benchmark</title>
<content type="html">&lt;p&gt;Trying to be a &lt;a href=&quot;http://www.sdtimes.com/content/article.aspx?ArticleID=31222&quot;&gt;cool kid&lt;/a&gt;, 
I&#8217;ve recently spent some time playing with &lt;a href=&quot;http://www.erlang.org/&quot;&gt;Erlang&lt;/a&gt;.
So I&#8217;ve found in Joe Armstrong’s &lt;a href=&quot;http://www.pragprog.com/titles/jaerlang/programming-erlang&quot;&gt;Programming Erlang&lt;/a&gt; 
the following exercise:&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;&#8221;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.&lt;/p&gt;
    
    &lt;p&gt;Write a similar program in some other programming language you are familiar with. Compare the results. Write a blog, and publish the results on the Internet! &#8221;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;(Already a &lt;a href=&quot;http://www.google.com/search?hl=en&amp;amp;amp;q=erlang+ring+benchmark&quot;&gt;classic&lt;/a&gt; now &#8230;).&lt;/p&gt;

&lt;p&gt;I dutifully complied: here&#8217;s my solution. &lt;/p&gt;

&lt;table class=&quot;CodeRay&quot;&gt;&lt;tr&gt;
  &lt;td title=&quot;click to toggle&quot; class=&quot;line_numbers&quot;&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;26&lt;tt&gt;
&lt;/tt&gt;27&lt;tt&gt;
&lt;/tt&gt;28&lt;tt&gt;
&lt;/tt&gt;29&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;30&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;31&lt;tt&gt;
&lt;/tt&gt;32&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class=&quot;code&quot;&gt;&lt;pre&gt;-&lt;span class=&quot;r&quot;&gt;module&lt;/span&gt;(ringu).&lt;tt&gt;
&lt;/tt&gt;-export([start/&lt;span class=&quot;i&quot;&gt;2&lt;/span&gt;]).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;% ----------------------------------------------------------------------&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;start(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;) -&amp;gt; &lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt; = &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;(),&lt;tt&gt;
&lt;/tt&gt;    statistics(runtime),&lt;tt&gt;
&lt;/tt&gt;    statistics(wall_clock),&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt; = spawn(fun() -&amp;gt; ringu(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;(), &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;),&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt; ! x,&lt;tt&gt;
&lt;/tt&gt;    receive &lt;tt&gt;
&lt;/tt&gt;        _ -&amp;gt; &lt;span class=&quot;pc&quot;&gt;true&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;,&lt;tt&gt;
&lt;/tt&gt;    {_, &lt;span class=&quot;co&quot;&gt;TCPU&lt;/span&gt;} = statistics(runtime),&lt;tt&gt;
&lt;/tt&gt;    {_, &lt;span class=&quot;co&quot;&gt;Time&lt;/span&gt;} = statistics(wall_clock),&lt;tt&gt;
&lt;/tt&gt;    io&lt;span class=&quot;sy&quot;&gt;:format&lt;/span&gt;(&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;~nElapsed time: ~w ms~nCPU time:     ~w ms ~n&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; ,&lt;tt&gt;
&lt;/tt&gt;          [&lt;span class=&quot;co&quot;&gt;Time&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;TCPU&lt;/span&gt;]).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;% ----------------------------------------------------------------------&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;ringu(&lt;span class=&quot;i&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) -&amp;gt; ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;), &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt; ! itsover;&lt;tt&gt;
&lt;/tt&gt;ringu(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) -&amp;gt; &lt;tt&gt;
&lt;/tt&gt;    ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, spawn(fun() -&amp;gt; ringu(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;i&quot;&gt;-1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;)).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;ringu_loop(&lt;span class=&quot;i&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;) -&amp;gt; ringu_fwd_msg(&lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;);   &lt;tt&gt;
&lt;/tt&gt;ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;) -&amp;gt; ringu_fwd_msg(&lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;), ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;i&quot;&gt;-1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;ringu_fwd_msg(&lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;) -&amp;gt;    &lt;tt&gt;
&lt;/tt&gt;    receive &lt;tt&gt;
&lt;/tt&gt;        &lt;span class=&quot;co&quot;&gt;X&lt;/span&gt; -&amp;gt; &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt; ! &lt;span class=&quot;co&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;%,&lt;/span&gt;&lt;span class=&quot;k&quot;&gt; io:format(&amp;quot;.&amp;quot;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;,&lt;/span&gt;&lt;/span&gt; [])&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;.&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Benchmark results, and an (uneven :) ) comparison with an equivalent script in Ruby &lt;a href=&quot;http://people.equars.com/2008/5/22/ruby-fiber-ring-benchmark&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://people.equars.com/">
    <author>
      <name>enrico</name>
    </author>
    <id>tag:people.equars.com,2008-05-22:186:9</id>
    <updated>2008-05-22T15:26:53Z</updated>
    <link href="http://people.equars.com/2008/12/1/erlang-ring-benchmark" rel="alternate" type="text/html"/>
    <title>Erlang ring benchmark</title>
<content type="html">&lt;p&gt;Trying to be a &lt;a href=&quot;http://www.sdtimes.com/content/article.aspx?ArticleID=31222&quot;&gt;cool kid&lt;/a&gt;, 
I&#8217;ve recently spent some time playing with &lt;a href=&quot;http://www.erlang.org/&quot;&gt;Erlang&lt;/a&gt;.
So I&#8217;ve found in Joe Armstrong’s &lt;a href=&quot;http://www.pragprog.com/titles/jaerlang/programming-erlang&quot;&gt;Programming Erlang&lt;/a&gt; 
the following exercise:&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;&#8221;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.&lt;/p&gt;
    
    &lt;p&gt;Write a similar program in some other programming language you are familiar with. Compare the results. Write a blog, and publish the results on the Internet! &#8221;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;(Already a &lt;a href=&quot;http://www.google.com/search?hl=en&amp;amp;amp;q=erlang+ring+benchmark&quot;&gt;classic&lt;/a&gt; now &#8230;).&lt;/p&gt;

&lt;p&gt;I dutifully complied: here&#8217;s my solution. &lt;/p&gt;

&lt;table class=&quot;CodeRay&quot;&gt;&lt;tr&gt;
  &lt;td title=&quot;click to toggle&quot; class=&quot;line_numbers&quot;&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;26&lt;tt&gt;
&lt;/tt&gt;27&lt;tt&gt;
&lt;/tt&gt;28&lt;tt&gt;
&lt;/tt&gt;29&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;30&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;31&lt;tt&gt;
&lt;/tt&gt;32&lt;tt&gt;
&lt;/tt&gt;33&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class=&quot;code&quot;&gt;&lt;pre&gt;-&lt;span class=&quot;r&quot;&gt;module&lt;/span&gt;(ringu).&lt;tt&gt;
&lt;/tt&gt;-export([start/&lt;span class=&quot;i&quot;&gt;2&lt;/span&gt;]).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;% ----------------------------------------------------------------------&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;start(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;) -&amp;gt; &lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt; = &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;(),&lt;tt&gt;
&lt;/tt&gt;    statistics(runtime),&lt;tt&gt;
&lt;/tt&gt;    statistics(wall_clock),&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt; = spawn(fun() -&amp;gt; ringu(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;(), &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;),&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt; ! x,&lt;tt&gt;
&lt;/tt&gt;    receive &lt;tt&gt;
&lt;/tt&gt;        _ -&amp;gt; &lt;span class=&quot;pc&quot;&gt;true&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;,&lt;tt&gt;
&lt;/tt&gt;    {_, &lt;span class=&quot;co&quot;&gt;TCPU&lt;/span&gt;} = statistics(runtime),&lt;tt&gt;
&lt;/tt&gt;    {_, &lt;span class=&quot;co&quot;&gt;Time&lt;/span&gt;} = statistics(wall_clock),&lt;tt&gt;
&lt;/tt&gt;    io&lt;span class=&quot;sy&quot;&gt;:format&lt;/span&gt;(&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;~nElapsed time: ~w ms~nCPU time:     ~w ms ~n&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; ,&lt;tt&gt;
&lt;/tt&gt;          [&lt;span class=&quot;co&quot;&gt;Time&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;TCPU&lt;/span&gt;]).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;% ----------------------------------------------------------------------&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;ringu(&lt;span class=&quot;i&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) -&amp;gt; ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;), &lt;tt&gt;
&lt;/tt&gt;                                          &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt; ! itsover;&lt;tt&gt;
&lt;/tt&gt;ringu(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) -&amp;gt; &lt;tt&gt;
&lt;/tt&gt;    ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, spawn(fun() -&amp;gt; ringu(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;i&quot;&gt;-1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;)).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;ringu_loop(&lt;span class=&quot;i&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;) -&amp;gt; ringu_fwd_msg(&lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;);   &lt;tt&gt;
&lt;/tt&gt;ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;) -&amp;gt; ringu_fwd_msg(&lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;), ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;i&quot;&gt;-1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;ringu_fwd_msg(&lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;) -&amp;gt;    &lt;tt&gt;
&lt;/tt&gt;    receive &lt;tt&gt;
&lt;/tt&gt;        &lt;span class=&quot;co&quot;&gt;X&lt;/span&gt; -&amp;gt; &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt; ! &lt;span class=&quot;co&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;%,&lt;/span&gt;&lt;span class=&quot;k&quot;&gt; io:format(&amp;quot;.&amp;quot;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;,&lt;/span&gt;&lt;/span&gt; [])&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;.&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Benchmark results, and an (uneven :) ) comparison with an equivalent script in Ruby &lt;a href=&quot;http://people.equars.com/2008/5/22/ruby-fiber-ring-benchmark&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://people.equars.com/">
    <author>
      <name>enrico</name>
    </author>
    <id>tag:people.equars.com,2008-05-22:185:8</id>
    <updated>2008-05-22T15:24:45Z</updated>
    <link href="http://people.equars.com/2008/12/1/erlang-ring-benchmark" rel="alternate" type="text/html"/>
    <title>Erlang ring benchmark</title>
<content type="html">&lt;p&gt;Trying to be a &lt;a href=&quot;http://www.sdtimes.com/content/article.aspx?ArticleID=31222&quot;&gt;cool kid&lt;/a&gt;, 
I&#8217;ve recently spent some time playing with &lt;a href=&quot;http://www.erlang.org/&quot;&gt;Erlang&lt;/a&gt;.
So I&#8217;ve found in Joe Armstrong’s &lt;a href=&quot;http://www.pragprog.com/titles/jaerlang/programming-erlang&quot;&gt;Programming Erlang&lt;/a&gt; 
the following exercise:&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;&#8221;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.&lt;/p&gt;
    
    &lt;p&gt;Write a similar program in some other programming language you are familiar with. Compare the results. Write a blog, and publish the results on the Internet! &#8221;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;(Already a &lt;a href=&quot;http://www.google.com/search?hl=en&amp;amp;amp;q=erlang+ring+benchmark&quot;&gt;classic&lt;/a&gt; now &#8230;).&lt;/p&gt;

&lt;p&gt;I dutifully complied: here&#8217;s my solution. &lt;/p&gt;

&lt;table class=&quot;CodeRay&quot;&gt;&lt;tr&gt;
  &lt;td title=&quot;click to toggle&quot; class=&quot;line_numbers&quot;&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;26&lt;tt&gt;
&lt;/tt&gt;27&lt;tt&gt;
&lt;/tt&gt;28&lt;tt&gt;
&lt;/tt&gt;29&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;30&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;31&lt;tt&gt;
&lt;/tt&gt;32&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class=&quot;code&quot;&gt;&lt;pre&gt;-&lt;span class=&quot;r&quot;&gt;module&lt;/span&gt;(ringu).&lt;tt&gt;
&lt;/tt&gt;-export([start/&lt;span class=&quot;i&quot;&gt;2&lt;/span&gt;]).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;% ----------------------------------------------------------------------&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;start(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;) -&amp;gt; &lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt; = &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;(),&lt;tt&gt;
&lt;/tt&gt;    statistics(runtime),&lt;tt&gt;
&lt;/tt&gt;    %statistics(wall_clock),&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt; = spawn(fun() -&amp;gt; ringu(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;(), &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;),&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt; ! x,&lt;tt&gt;
&lt;/tt&gt;    receive &lt;tt&gt;
&lt;/tt&gt;        _ -&amp;gt; &lt;span class=&quot;pc&quot;&gt;true&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;,&lt;tt&gt;
&lt;/tt&gt;    {_, &lt;span class=&quot;co&quot;&gt;TCPU&lt;/span&gt;} = statistics(runtime),&lt;tt&gt;
&lt;/tt&gt;    {_, &lt;span class=&quot;co&quot;&gt;Time&lt;/span&gt;} = statistics(wall_clock),&lt;tt&gt;
&lt;/tt&gt;    io&lt;span class=&quot;sy&quot;&gt;:format&lt;/span&gt;(&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;~nElapsed time: ~w ms~nCPU time:     ~w ms ~n&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; ,&lt;tt&gt;
&lt;/tt&gt;          [&lt;span class=&quot;co&quot;&gt;Time&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;TCPU&lt;/span&gt;]).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;% ----------------------------------------------------------------------&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;ringu(&lt;span class=&quot;i&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) -&amp;gt; ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;), &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt; ! itsover;&lt;tt&gt;
&lt;/tt&gt;ringu(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) -&amp;gt; &lt;tt&gt;
&lt;/tt&gt;    ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, spawn(fun() -&amp;gt; ringu(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;i&quot;&gt;-1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;)).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;ringu_loop(&lt;span class=&quot;i&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;) -&amp;gt; ringu_fwd_msg(&lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;);   &lt;tt&gt;
&lt;/tt&gt;ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;) -&amp;gt; ringu_fwd_msg(&lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;), ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;i&quot;&gt;-1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;ringu_fwd_msg(&lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;) -&amp;gt;    &lt;tt&gt;
&lt;/tt&gt;    receive &lt;tt&gt;
&lt;/tt&gt;        &lt;span class=&quot;co&quot;&gt;X&lt;/span&gt; -&amp;gt; &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt; ! &lt;span class=&quot;co&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;%,&lt;/span&gt;&lt;span class=&quot;k&quot;&gt; io:format(&amp;quot;.&amp;quot;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;,&lt;/span&gt;&lt;/span&gt; [])&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;.&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Benchmark results, and an (uneven :) ) comparison with an equivalent script in Ruby &lt;a href=&quot;http://people.equars.com/2008/5/22/ruby-fiber-ring-benchmark&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://people.equars.com/">
    <author>
      <name>enrico</name>
    </author>
    <id>tag:people.equars.com,2008-05-22:184:7</id>
    <updated>2008-05-22T15:22:31Z</updated>
    <link href="http://people.equars.com/2008/12/1/erlang-ring-benchmark" rel="alternate" type="text/html"/>
    <title>Erlang ring benchmark</title>
<content type="html">&lt;p&gt;Trying to be a &lt;a href=&quot;http://www.sdtimes.com/content/article.aspx?ArticleID=31222&quot;&gt;cool kid&lt;/a&gt;, 
I&#8217;ve recently spent some time playing with &lt;a href=&quot;http://www.erlang.org/&quot;&gt;Erlang&lt;/a&gt;.
So I&#8217;ve found in Joe Armstrong’s &lt;a href=&quot;http://www.pragprog.com/titles/jaerlang/programming-erlang&quot;&gt;Programming Erlang&lt;/a&gt; 
the following exercise:&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;&#8221;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.&lt;/p&gt;
    
    &lt;p&gt;Write a similar program in some other programming language you are familiar with. Compare the results. Write a blog, and publish the results on the Internet! &#8221;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;(Already a &lt;a href=&quot;http://www.google.com/search?hl=en&amp;amp;amp;q=erlang+ring+benchmark&quot;&gt;classic&lt;/a&gt; now &#8230;).&lt;/p&gt;

&lt;p&gt;I dutifully complied: here&#8217;s my solution. &lt;/p&gt;

&lt;table class=&quot;CodeRay&quot;&gt;&lt;tr&gt;
  &lt;td title=&quot;click to toggle&quot; class=&quot;line_numbers&quot;&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;26&lt;tt&gt;
&lt;/tt&gt;27&lt;tt&gt;
&lt;/tt&gt;28&lt;tt&gt;
&lt;/tt&gt;29&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;30&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;31&lt;tt&gt;
&lt;/tt&gt;32&lt;tt&gt;
&lt;/tt&gt;33&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class=&quot;code&quot;&gt;&lt;pre&gt;-&lt;span class=&quot;r&quot;&gt;module&lt;/span&gt;(ringu).&lt;tt&gt;
&lt;/tt&gt;-export([start/&lt;span class=&quot;i&quot;&gt;2&lt;/span&gt;]).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;% ----------------------------------------------------------------------&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;start(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;) -&amp;gt; &lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt; = &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;(),&lt;tt&gt;
&lt;/tt&gt;    statistics(runtime),&lt;tt&gt;
&lt;/tt&gt;    %statistics(wall_clock),&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt; = spawn(fun() -&amp;gt; ringu(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;(), &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;),&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt; ! x,&lt;tt&gt;
&lt;/tt&gt;    receive &lt;tt&gt;
&lt;/tt&gt;        _ -&amp;gt; &lt;span class=&quot;pc&quot;&gt;true&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;,&lt;tt&gt;
&lt;/tt&gt;    {_, &lt;span class=&quot;co&quot;&gt;TCPU&lt;/span&gt;} = statistics(runtime),&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;%{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;_, Time&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;}&lt;/span&gt;&lt;/span&gt; = statistics(wall_clock),&lt;tt&gt;
&lt;/tt&gt;    %io&lt;span class=&quot;sy&quot;&gt;:format&lt;/span&gt;(&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;~nElapsed time: ~w ms~nCPU time:     ~w ms ~n&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; ,&lt;tt&gt;
&lt;/tt&gt;    %      [&lt;span class=&quot;co&quot;&gt;Time&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;TCPU&lt;/span&gt;]).&lt;tt&gt;
&lt;/tt&gt;    io&lt;span class=&quot;sy&quot;&gt;:format&lt;/span&gt;(&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;~w&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, [&lt;span class=&quot;co&quot;&gt;TCPU&lt;/span&gt;]).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;% ----------------------------------------------------------------------&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;ringu(&lt;span class=&quot;i&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) -&amp;gt; ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;), &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt; ! itsover;&lt;tt&gt;
&lt;/tt&gt;ringu(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) -&amp;gt; &lt;tt&gt;
&lt;/tt&gt;    ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, spawn(fun() -&amp;gt; ringu(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;i&quot;&gt;-1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;)).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;ringu_loop(&lt;span class=&quot;i&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;) -&amp;gt; ringu_fwd_msg(&lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;);   &lt;tt&gt;
&lt;/tt&gt;ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;) -&amp;gt; ringu_fwd_msg(&lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;), ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;i&quot;&gt;-1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;ringu_fwd_msg(&lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;) -&amp;gt;    &lt;tt&gt;
&lt;/tt&gt;    receive &lt;tt&gt;
&lt;/tt&gt;        &lt;span class=&quot;co&quot;&gt;X&lt;/span&gt; -&amp;gt; &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt; ! &lt;span class=&quot;co&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;%,&lt;/span&gt;&lt;span class=&quot;k&quot;&gt; io:format(&amp;quot;.&amp;quot;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;,&lt;/span&gt;&lt;/span&gt; [])&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;.&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Benchmark results, and an (uneven :) ) comparison with an equivalent script in Ruby &lt;a href=&quot;http://people.equars.com/2008/5/22/ruby-fiber-ring-benchmark&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://people.equars.com/">
    <author>
      <name>enrico</name>
    </author>
    <id>tag:people.equars.com,2008-05-22:183:6</id>
    <updated>2008-05-22T15:22:11Z</updated>
    <link href="http://people.equars.com/2008/12/1/erlang-ring-benchmark" rel="alternate" type="text/html"/>
    <title>Erlang ring benchmark</title>
<content type="html">&lt;p&gt;Trying to be a &lt;a href=&quot;http://www.sdtimes.com/content/article.aspx?ArticleID=31222&quot;&gt;cool kid&lt;/a&gt;, 
I&#8217;ve recently spent some time playing with &lt;a href=&quot;http://www.erlang.org/&quot;&gt;Erlang&lt;/a&gt;.
So I&#8217;ve found in Joe Armstrong’s &lt;a href=&quot;http://www.pragprog.com/titles/jaerlang/programming-erlang&quot;&gt;Programming Erlang&lt;/a&gt; 
the following exercise:&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;&#8221;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.&lt;/p&gt;
    
    &lt;p&gt;Write a similar program in some other programming language you are familiar with. Compare the results. Write a blog, and publish the results on the Internet! &#8221;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;(Already a [classic][http://www.google.com/search?hl=en&amp;amp;q=erlang+ring+benchmark] now &#8230;).&lt;/p&gt;

&lt;p&gt;I dutifully complied: here&#8217;s my solution. &lt;/p&gt;

&lt;table class=&quot;CodeRay&quot;&gt;&lt;tr&gt;
  &lt;td title=&quot;click to toggle&quot; class=&quot;line_numbers&quot;&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;26&lt;tt&gt;
&lt;/tt&gt;27&lt;tt&gt;
&lt;/tt&gt;28&lt;tt&gt;
&lt;/tt&gt;29&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;30&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;31&lt;tt&gt;
&lt;/tt&gt;32&lt;tt&gt;
&lt;/tt&gt;33&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class=&quot;code&quot;&gt;&lt;pre&gt;-&lt;span class=&quot;r&quot;&gt;module&lt;/span&gt;(ringu).&lt;tt&gt;
&lt;/tt&gt;-export([start/&lt;span class=&quot;i&quot;&gt;2&lt;/span&gt;]).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;% ----------------------------------------------------------------------&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;start(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;) -&amp;gt; &lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt; = &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;(),&lt;tt&gt;
&lt;/tt&gt;    statistics(runtime),&lt;tt&gt;
&lt;/tt&gt;    %statistics(wall_clock),&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt; = spawn(fun() -&amp;gt; ringu(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;(), &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;),&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt; ! x,&lt;tt&gt;
&lt;/tt&gt;    receive &lt;tt&gt;
&lt;/tt&gt;        _ -&amp;gt; &lt;span class=&quot;pc&quot;&gt;true&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;,&lt;tt&gt;
&lt;/tt&gt;    {_, &lt;span class=&quot;co&quot;&gt;TCPU&lt;/span&gt;} = statistics(runtime),&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;%{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;_, Time&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;}&lt;/span&gt;&lt;/span&gt; = statistics(wall_clock),&lt;tt&gt;
&lt;/tt&gt;    %io&lt;span class=&quot;sy&quot;&gt;:format&lt;/span&gt;(&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;~nElapsed time: ~w ms~nCPU time:     ~w ms ~n&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; ,&lt;tt&gt;
&lt;/tt&gt;    %      [&lt;span class=&quot;co&quot;&gt;Time&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;TCPU&lt;/span&gt;]).&lt;tt&gt;
&lt;/tt&gt;    io&lt;span class=&quot;sy&quot;&gt;:format&lt;/span&gt;(&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;~w&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, [&lt;span class=&quot;co&quot;&gt;TCPU&lt;/span&gt;]).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;% ----------------------------------------------------------------------&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;ringu(&lt;span class=&quot;i&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) -&amp;gt; ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;), &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt; ! itsover;&lt;tt&gt;
&lt;/tt&gt;ringu(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) -&amp;gt; &lt;tt&gt;
&lt;/tt&gt;    ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, spawn(fun() -&amp;gt; ringu(&lt;span class=&quot;co&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;i&quot;&gt;-1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid_0&lt;/span&gt;) &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;)).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;ringu_loop(&lt;span class=&quot;i&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;) -&amp;gt; ringu_fwd_msg(&lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;);   &lt;tt&gt;
&lt;/tt&gt;ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;) -&amp;gt; ringu_fwd_msg(&lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;), ringu_loop(&lt;span class=&quot;co&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;i&quot;&gt;-1&lt;/span&gt;, &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;).&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;ringu_fwd_msg(&lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt;) -&amp;gt;    &lt;tt&gt;
&lt;/tt&gt;    receive &lt;tt&gt;
&lt;/tt&gt;        &lt;span class=&quot;co&quot;&gt;X&lt;/span&gt; -&amp;gt; &lt;span class=&quot;co&quot;&gt;Pid&lt;/span&gt; ! &lt;span class=&quot;co&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;%,&lt;/span&gt;&lt;span class=&quot;k&quot;&gt; io:format(&amp;quot;.&amp;quot;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;,&lt;/span&gt;&lt;/span&gt; [])&lt;tt&gt;
&lt;/tt&gt;    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;.&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Benchmark results, and an (uneven :) ) comparison with an equivalent script in Ruby &lt;a href=&quot;http://people.equars.com/2008/5/22/ruby-fiber-ring-benchmark&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content>  </entry>
</feed>
