<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>A Tempest of Thoughts &#187; Erlang</title>
	<atom:link href="http://tempe.st/category/erlang/feed/" rel="self" type="application/rss+xml" />
	<link>http://tempe.st</link>
	<description>aka blog.to_int(:inig)</description>
	<lastBuildDate>Thu, 07 Apr 2011 08:24:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>How I beat Project Euler using Erlang: Christmas Edition, problem one</title>
		<link>http://tempe.st/2007/12/how-i-beat-project-euler-using-erlang-christmas-edition-problem-one/</link>
		<comments>http://tempe.st/2007/12/how-i-beat-project-euler-using-erlang-christmas-edition-problem-one/#comments</comments>
		<pubDate>Wed, 26 Dec 2007 11:52:56 +0000</pubDate>
		<dc:creator>Giovanni Intini</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Euler]]></category>

		<guid isPermaLink="false">http://tempe.st/2007/12/how-i-beat-project-euler-using-erlang-christmas-edition-problem-one/</guid>
		<description><![CDATA[Merry Christmas everyone! It&#8217;s been a long time since I wrote a post, but I&#8217;ve been very busy coding, and I could only muster some posts for Stacktrace, so express all your Christmas-y goodness and pardon me On Stacktrace we just started posting in depth analysis of the Project Euler problems, and I decided to [...]]]></description>
			<content:encoded><![CDATA[	<p>Merry Christmas everyone!</p>
	<p>It&#8217;s been a long time since I wrote a post, but I&#8217;ve been very busy coding, and I could only muster some posts for <a href="http://stacktrace.it">Stacktrace</a>, so express all your Christmas-y goodness and pardon me <img src='http://tempe.st/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
	<p>On Stacktrace <a href="http://stacktrace.it/articoli/2007/12/progetto-eulero-problema-1">we just started posting</a> in depth analysis of the <a href="http://projecteuler.net">Project Euler</a> problems, and I decided to try and solve them using Erlang, since I already did using Ruby a couple of years ago.</p>
	<p>The first problem is quite simple: </p>
	<blockquote>
		<p>If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. </p>
	</blockquote>
	<p>I will not try to give you the clever solution since this problem is quite simple, and the brute force solutions works fine <img src='http://tempe.st/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #014ea4;">-</span><span style="color: #5400b3;">module</span><span style="color: #109ab8;">&#40;</span>p1<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span> 
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">export</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span>sum_mul<span style="color: #014ea4;">/</span><span style="color: #ff9600;">1</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span> 
&nbsp;
<span style="color: #ff3c00;">sum_mul</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">H</span>|T<span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #ff3c00;">valid</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">H</span><span style="color: #109ab8;">&#41;</span> <span style="color: #014ea4;">+</span> <span style="color: #ff3c00;">sum_mul</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">T</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span>
<span style="color: #ff3c00;">sum_mul</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #ff9600;">0</span><span style="color: #6bb810;">.</span>
<span style="color: #ff3c00;">valid</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">H</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">when</span> <span style="color: #45b3e6;">H</span> <span style="color: #014ea4;">rem</span> <span style="color: #ff9600;">3</span> <span style="color: #014ea4;">=:=</span> <span style="color: #ff9600;">0</span><span style="color: #6bb810;">;</span> <span style="color: #45b3e6;">H</span> <span style="color: #014ea4;">rem</span> <span style="color: #ff9600;">5</span> <span style="color: #014ea4;">=:=</span> <span style="color: #ff9600;">0</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #45b3e6;">H</span><span style="color: #6bb810;">;</span>
<span style="color: #ff3c00;">valid</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">H</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #ff9600;">0</span><span style="color: #6bb810;">.</span></pre></div></div>

	<p>To get the solution just call _sum_mul(lists:seq(1,999))_.</p>
	<p>Have a Merry Erlang Christmas!</p>

 ]]></content:encoded>
			<wfw:commentRss>http://tempe.st/2007/12/how-i-beat-project-euler-using-erlang-christmas-edition-problem-one/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Erlang, Ruby and PHP battle it out!</title>
		<link>http://tempe.st/2007/05/erlang-ruby-and-php-battle-it-out/</link>
		<comments>http://tempe.st/2007/05/erlang-ruby-and-php-battle-it-out/#comments</comments>
		<pubDate>Sat, 19 May 2007 10:21:23 +0000</pubDate>
		<dc:creator>Giovanni Intini</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://tempe.st/2007/05/erlang-ruby-and-php-battle-it-out/</guid>
		<description><![CDATA[Update: here&#8217;s the followup! Yesterday I attended PHP Day, in the beautiful city of Verona, and while going there by train, together with my friend Federico Feroldi, I told him that in a test I made a week ago Erlang was much slower than ruby in finding pythagorean triplets. That was a partial lie, since [...]]]></description>
			<content:encoded><![CDATA[	<p><strong>Update</strong>: <a href="http://tempe.st/2007/05/the-battle-of-the-languages-part-ii">here&#8217;s the followup!</a></p>
	<p>Yesterday I attended <a href="http://www.phpday.it/site">PHP Day</a>, in the beautiful city of Verona, and while going there by train, together with my friend <a href="http://www.pixzone.com/blog">Federico Feroldi</a>, I told him that in a test I made a week ago Erlang was much slower than ruby in finding <a href="http://en.wikipedia.org/wiki/Pythagorean_triple">pythagorean triplets</a>. </p>
	<p>That was a partial lie, since the ruby implementation was somewhat optimized, while the erlang implementation wasn&#8217;t at all. So we decided to rewrite both programs using the same optimized algorithm and then test their speeds.</p>
	<p>This is the ruby implementation of a method that counts the number of pythagorean triplets up to N:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> pythag<span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span>
  result = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span>..<span style="color:#9900CC;">n</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>c<span style="color:#006600; font-weight:bold;">|</span> 
    <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>...<span style="color:#9900CC;">c</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>b<span style="color:#006600; font-weight:bold;">|</span>
      a = <span style="color:#CC00FF; font-weight:bold;">Math</span>.<span style="color:#9900CC;">sqrt</span><span style="color:#006600; font-weight:bold;">&#40;</span>c<span style="color:#006600; font-weight:bold;">*</span>c <span style="color:#006600; font-weight:bold;">-</span> b<span style="color:#006600; font-weight:bold;">*</span>b<span style="color:#006600; font-weight:bold;">&#41;</span>
      result <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span>a.<span style="color:#9900CC;">to_i</span>, b, c<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">if</span> a.<span style="color:#9900CC;">to_i</span> == a 
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  result
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

	<p>Then we coded it in Erlang. The code I&#8217;m going to show you is more complex than the ruby code because we took advantage of the parallelization features of the Erlang language:</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #014ea4;">-</span><span style="color: #5400b3;">module</span><span style="color: #109ab8;">&#40;</span>pythag<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">export</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span>bard<span style="color: #014ea4;">/</span><span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span> pbard<span style="color: #014ea4;">/</span><span style="color: #ff9600;">1</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">my_is_integer</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">X</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #fa6fff;">round</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">X</span><span style="color: #109ab8;">&#41;</span> <span style="color: #014ea4;">==</span> <span style="color: #45b3e6;">X</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">pbard</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">N</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #ff3c00;">pmap</span><span style="color: #109ab8;">&#40;</span><span style="color: #186895;">fun</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">C</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> 
              <span style="color: #109ab8;">&#91;</span>
               <span style="color: #109ab8;">&#123;</span><span style="color: #45b3e6;">C</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">B</span><span style="color: #6bb810;">,</span> <span style="color: #fa6fff;">trunc</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">A</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#125;</span> <span style="color: #014ea4;">||</span> 
                <span style="color: #45b3e6;">B</span> <span style="color: #014ea4;">&lt;-</span> <span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">seq</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">C</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">N</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span> 
                <span style="color: #45b3e6;">A</span> <span style="color: #014ea4;">&lt;-</span> <span style="color: #109ab8;">&#91;</span><span style="color: #ff4e18;">math</span>:<span style="color: #ff3c00;">sqrt</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">C</span><span style="color: #014ea4;">*</span><span style="color: #45b3e6;">C</span> <span style="color: #014ea4;">+</span> <span style="color: #45b3e6;">B</span><span style="color: #014ea4;">*</span><span style="color: #45b3e6;">B</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#93;</span><span style="color: #6bb810;">,</span> <span style="color: #ff3c00;">my_is_integer</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">A</span><span style="color: #109ab8;">&#41;</span> 
              <span style="color: #109ab8;">&#93;</span>
            <span style="color: #186895;">end</span><span style="color: #6bb810;">,</span> 
            <span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">seq</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff9600;">2</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">N</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">bard</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">N</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">map</span><span style="color: #109ab8;">&#40;</span><span style="color: #186895;">fun</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">C</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> 
              <span style="color: #109ab8;">&#91;</span>
               <span style="color: #109ab8;">&#123;</span><span style="color: #45b3e6;">C</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">B</span><span style="color: #6bb810;">,</span> <span style="color: #fa6fff;">trunc</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">A</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#125;</span> <span style="color: #014ea4;">||</span> 
                <span style="color: #45b3e6;">B</span> <span style="color: #014ea4;">&lt;-</span> <span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">seq</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">C</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">N</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span> 
                <span style="color: #45b3e6;">A</span> <span style="color: #014ea4;">&lt;-</span> <span style="color: #109ab8;">&#91;</span><span style="color: #ff4e18;">math</span>:<span style="color: #ff3c00;">sqrt</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">C</span><span style="color: #014ea4;">*</span><span style="color: #45b3e6;">C</span> <span style="color: #014ea4;">+</span> <span style="color: #45b3e6;">B</span><span style="color: #014ea4;">*</span><span style="color: #45b3e6;">B</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#93;</span><span style="color: #6bb810;">,</span> <span style="color: #ff3c00;">my_is_integer</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">A</span><span style="color: #109ab8;">&#41;</span> 
              <span style="color: #109ab8;">&#93;</span>
            <span style="color: #186895;">end</span><span style="color: #6bb810;">,</span> 
            <span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">seq</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff9600;">2</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">N</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">pmap</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">F</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">L</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> 
  <span style="color: #45b3e6;">S</span> <span style="color: #014ea4;">=</span> <span style="color: #fa6fff;">self</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span> 
  <span style="color: #666666; font-style: italic;">%% make_ref() returns a unique reference </span>
  <span style="color: #666666; font-style: italic;">%% we'll match on this later </span>
  <span style="color: #45b3e6;">Ref</span> <span style="color: #014ea4;">=</span> <span style="color: #ff4e18;">erlang</span>:<span style="color: #fa6fff;">make_ref</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span> 
  <span style="color: #45b3e6;">Pids</span> <span style="color: #014ea4;">=</span> <span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">map</span><span style="color: #109ab8;">&#40;</span><span style="color: #186895;">fun</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">I</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> 
                    <span style="color: #fa6fff;">spawn</span><span style="color: #109ab8;">&#40;</span><span style="color: #186895;">fun</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #ff3c00;">do_f</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">S</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Ref</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">F</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">I</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">end</span><span style="color: #109ab8;">&#41;</span> 
             <span style="color: #186895;">end</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">L</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span> 
  <span style="color: #666666; font-style: italic;">%% gather the results </span>
  <span style="color: #ff3c00;">gather</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Pids</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Ref</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span> 
&nbsp;
<span style="color: #ff3c00;">do_f</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Parent</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Ref</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">F</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">I</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> 
  <span style="color: #45b3e6;">Parent</span> <span style="color: #014ea4;">!</span> <span style="color: #109ab8;">&#123;</span><span style="color: #fa6fff;">self</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Ref</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#40;</span><span style="color: #186895;">catch</span> <span style="color: #45b3e6;">F</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">I</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">.</span> 
<span style="color: #ff3c00;">gather</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Pid</span>|T<span style="color: #109ab8;">&#93;</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Ref</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> 
  <span style="color: #186895;">receive</span> 
    <span style="color: #109ab8;">&#123;</span><span style="color: #45b3e6;">Pid</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Ref</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Ret</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Ret</span>|<span style="color: #ff3c00;">gather</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">T</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Ref</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#93;</span> 
  <span style="color: #186895;">end</span><span style="color: #6bb810;">;</span> 
<span style="color: #ff3c00;">gather</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#93;</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">_</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> 
  <span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#93;</span><span style="color: #6bb810;">.</span></pre></div></div>

	<p>The pythag function has been named bard, as in <a href="http://hyperstruct.net">The Bard</a> <img src='http://tempe.st/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> <br />
The pbard function fires up a thread for each iteration of the outer loop and then gathers all the result back. This allows for up to 50% performance increase when using dual core CPUs (like my Macbook Pro <img src='http://tempe.st/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ).</p>
	<p>Once we coded it in two languages we couldn&#8217;t stop, so here&#8217;s a PHP implementation, a Javascript one and, last, but not least, a C implementation, because you can never forget C when you need speed <img src='http://tempe.st/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
	<p>PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">5000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">*</span><span style="color: #000088;">$c</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$b</span><span style="color: #339933;">*</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_int</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$i</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

	<p>JavaScript:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> pythag<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> a<span style="color: #339933;">,</span>b<span style="color: #339933;">,</span>c<span style="color: #339933;">,</span>i
  <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>c <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span> c <span style="color: #339933;">&lt;=</span> n<span style="color: #339933;">;</span> c<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>b <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> b <span style="color: #339933;">&lt;</span> c<span style="color: #339933;">;</span> b<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      a <span style="color: #339933;">=</span> Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#40;</span>c<span style="color: #339933;">*</span>c <span style="color: #339933;">-</span> b<span style="color: #339933;">*</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        i<span style="color: #339933;">++;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> i<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

	<p>C:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;math.h&gt;</span>
&nbsp;
<span style="color: #339933;">#define MAX 5000</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">double</span> c<span style="color: #339933;">,</span> b<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">double</span> a<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>c <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span> c <span style="color: #339933;">&lt;=</span> MAX<span style="color: #339933;">;</span> c<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>b <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> b <span style="color: #339933;">&lt;</span> c<span style="color: #339933;">;</span> b<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      a <span style="color: #339933;">=</span> sqrt<span style="color: #009900;">&#40;</span>c<span style="color: #339933;">*</span>c <span style="color: #339933;">-</span> b<span style="color: #339933;">*</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>trunc<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        i<span style="color: #339933;">++;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

	<p>To test the various relative speeds I ran them from the shell using time (php ruby and c), or used the internal timer (erlang), or ran them in firefox (js only actually).</p>
	<p>The results were interesting. At the first run, using 5000 as N in the pythag functions the order was:</p>
	<ol>
		<li>C (obviously): 0.40s</li>
		<li>Erlang (smp): 3.95s</li>
		<li>Erlang (non-smp): 5.66s</li>
		<li>Ruby: 15.31s</li>
		<li>PHP: 19s (or so)</li>
		<li>Javascript: couldn&#8217;t even finish running because firefox said it was taking too much time</li>
	</ol>
	<p><a href="http://fullo.net">Fullo</a> passed by and asked what we were doing, I told him I was testing languages and that PHP was dog-slow. He said it couldn&#8217;t be and asked me what version of PHP I was using. I checked and it was PHP 4.x.x. </p>
	<p>After downloading and compiling PHP5 I ran the tests again and the final result was this:</p>
	<ol>
		<li>C (obviously): 0.40s</li>
		<li>Erlang (smp): 3.95s</li>
		<li>Erlang (non-smp): 5.66s</li>
		<li>PHP5: 8.9s (or so)</li>
		<li>Ruby: 15.31s</li>
		<li>Javascript: couldn&#8217;t even finish running because firefox said it was taking too much time</li>
	</ol>
	<p>Now the numbers make sense. Doing these comparations was a nice experience, because when you implement the same thing in different languages you always learn something about optimization and code beauty.</p>


 ]]></content:encoded>
			<wfw:commentRss>http://tempe.st/2007/05/erlang-ruby-and-php-battle-it-out/feed/</wfw:commentRss>
		<slash:comments>48</slash:comments>
		</item>
		<item>
		<title>Learning Erlang: The Beautiful Quicksort</title>
		<link>http://tempe.st/2007/04/learning-erlang-the-beautiful-quicksort/</link>
		<comments>http://tempe.st/2007/04/learning-erlang-the-beautiful-quicksort/#comments</comments>
		<pubDate>Tue, 03 Apr 2007 11:02:42 +0000</pubDate>
		<dc:creator>Giovanni Intini</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Learning Erlang]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://tempe.st/2007/04/learning-erlang-the-beautiful-quicksort/</guid>
		<description><![CDATA[Sometimes you really have to stop and contemplate how beautiful code is. This is an erlang quicksort algorithm, one of the most efficient (if I remember correctly it&#8217;s the most efficient) sorting algorithms. I still remember the pains of implementing it in C. qsort&#40;&#91;&#93;&#41; -&#62; &#91;&#93;; qsort&#40;&#91;Pivot&#124;T&#93;&#41; -&#62; qsort&#40;&#91;X &#124;&#124; X &#60;- T, X =&#60; [...]]]></description>
			<content:encoded><![CDATA[	<p>Sometimes you really have to stop and contemplate how beautiful code is. This is an erlang quicksort algorithm, one of the most efficient (if I remember correctly it&#8217;s the most efficient) sorting algorithms. I still remember the pains of implementing it in C.</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #ff3c00;">qsort</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#93;</span><span style="color: #6bb810;">;</span>
<span style="color: #ff3c00;">qsort</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Pivot</span>|T<span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
	<span style="color: #ff3c00;">qsort</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">X</span> <span style="color: #014ea4;">||</span> <span style="color: #45b3e6;">X</span> <span style="color: #014ea4;">&lt;-</span> <span style="color: #45b3e6;">T</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">X</span> <span style="color: #014ea4;">=&lt;</span> <span style="color: #45b3e6;">Pivot</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span>
	<span style="color: #014ea4;">++</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Pivot</span><span style="color: #109ab8;">&#93;</span> <span style="color: #014ea4;">++</span>
	<span style="color: #ff3c00;">qsort</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">X</span> <span style="color: #014ea4;">||</span> <span style="color: #45b3e6;">X</span> <span style="color: #014ea4;">&lt;-</span> <span style="color: #45b3e6;">T</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">X</span> <span style="color: #014ea4;">&gt;</span> <span style="color: #45b3e6;">Pivot</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span></pre></div></div>

	<p>Short + Clean + Elegant = Beautiful.</p>

 ]]></content:encoded>
			<wfw:commentRss>http://tempe.st/2007/04/learning-erlang-the-beautiful-quicksort/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Erlang and Poker</title>
		<link>http://tempe.st/2007/03/erlang-and-poker/</link>
		<comments>http://tempe.st/2007/03/erlang-and-poker/#comments</comments>
		<pubDate>Mon, 26 Mar 2007 12:14:29 +0000</pubDate>
		<dc:creator>Giovanni Intini</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://tempe.st/2007/03/erlang-and-poker/</guid>
		<description><![CDATA[Just when I started writing some poker odds analysis code in Erlang, a friend sent me this link. Sometimes it looks like the Universe really wants you to do something, isn&#8217;t it?]]></description>
			<content:encoded><![CDATA[	<p>Just when I started writing some poker odds analysis code in Erlang, a friend sent me <a href="http://wagerlabs.com/2007/3/5/erlang-lisp-and-the-scalable-ai-battle">this link</a>. Sometimes it looks like the Universe really wants you to do something, isn&#8217;t it? <img src='http://tempe.st/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

 ]]></content:encoded>
			<wfw:commentRss>http://tempe.st/2007/03/erlang-and-poker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning Erlang, day 1: No Variables?</title>
		<link>http://tempe.st/2007/03/learning-erlang-day-1-no-variables/</link>
		<comments>http://tempe.st/2007/03/learning-erlang-day-1-no-variables/#comments</comments>
		<pubDate>Thu, 15 Mar 2007 09:13:23 +0000</pubDate>
		<dc:creator>Giovanni Intini</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Learning Erlang]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://tempe.st/2007/03/learning-erlang-day-1-no-variables/</guid>
		<description><![CDATA[I first heard of Erlang from SamePlace creator, Massimiliano Mirra. He mostly praised the functional nature of the language and its performance. I always listen to my friends, so I made a mental note, and every once in a while I tried to start with Erlang, but other stuff(tm) always got the priority. A few [...]]]></description>
			<content:encoded><![CDATA[	<p>I first heard of Erlang from <a href="http://sameplace.cc">SamePlace</a> creator, Massimiliano Mirra. He mostly praised the functional nature of the language and its performance.</p>
	<p>I always listen to my friends, so I made a mental note, and every once in a while I tried to start with Erlang, but <em>other stuff</em>(tm) always got the priority.</p>
	<p>A few days ago, generating a burst of blog posts, the <a href="http://pragmaticprogrammer.com">Pragmatic Programmers</a>  released  the beta version of  <a href="http://www.pragmaticprogrammer.com/titles/jaerlang/">Programming Erlang</a>. This was not a coincidence. When both Massimiliano and the Pragmatic Programmers suggest you should learn a new language you better do it, and be happy <img src='http://tempe.st/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
	<p>So I went to the pragprog website and looked through the table of contents. It was really interesting and I bought the beta book.</p>
	<p>Yesterday I got my hands wet with erlangness for the first time and decided to create a new category of posts, <a href="http://tempe.st/category/learning-erlang">Learning Erlang</a>, that will be filled with my observation about my efforts to learn a functional language after all these years of object orientedness.</p>
	<p>The first thing you will notice when working with erlang is that there are <strong>no variables</strong>. Ok, stop, don&#8217;t close the browser, it&#8217;s not entirely true, there are variables, but you can assign them only once. </p>
	<p>To better explain how erlang treats variables I&#8217;ll show you a snippet taken from the erlang interactive interpreter, <em>erl</em>:</p>
<pre>
Eshell V5.5.3  (abort with ^G)
1&gt; X = 45.
45
2&gt; X.
45
3&gt; X = 23.
	<p>=ERROR REPORT==== 15-Mar-2007::09:55:53 ===<br />
Error in process &lt;0.29.0&gt; with exit value: {{badmatch,23},[{erl_eval,expr,3}]}</p>
	<ul>
		<li>exited: {{badmatch,23},[{erl_eval,expr,3}]} **
4&gt; X = 45.
45
</pre>
	<p>Line 1 just assigns the <em>X</em> variable (in erlang variables start with a capital letter, and statements end with a period). What erlang really does here is matching <em>X</em> with 45. It notices that <em>X</em> is unbound and does whatever it can to make the statement true, so it binds <em>X</em> to 45. In line 2 we just check what <em>X</em> is bound to. In line 3 we learn how erlang works by trying to match <em>X</em> (now 45) to 23. <strong>Boom!</strong> nasty error for us. Being very scared, in line 4 we try to match <em>X</em> to 45, just in case, and we notice erlang likes that statement.</p>
	<p>I promise that once you get used to this way of handling variables it doesn&#8217;t look so strange anymore <img src='http://tempe.st/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>

 ]]></content:encoded>
			<wfw:commentRss>http://tempe.st/2007/03/learning-erlang-day-1-no-variables/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

