<?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; Javascript</title>
	<atom:link href="http://tempe.st/category/javascript/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>Rails and JavaScript: page.call gotchas</title>
		<link>http://tempe.st/2007/08/rails-and-javascript-pagecall-gotchas/</link>
		<comments>http://tempe.st/2007/08/rails-and-javascript-pagecall-gotchas/#comments</comments>
		<pubDate>Fri, 31 Aug 2007 13:38:17 +0000</pubDate>
		<dc:creator>Giovanni Intini</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Prototype.js]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://tempe.st/2007/08/rails-and-javascript-pagecall-gotchas/</guid>
		<description><![CDATA[Ruby on Rails has wonderful out of the box javascript support, but sometimes implementing dynamic user interfaces is not so easy as it seems. In an application I&#8217;m working on I have a list of people with a checkbox each. In the load event of the page I add a click handler to every checkbox [...]]]></description>
			<content:encoded><![CDATA[	<p>Ruby on Rails has wonderful out of the box javascript support, but sometimes implementing dynamic user interfaces is not so easy as it seems.</p>
	<p>In an application I&#8217;m working on I have a list of people with a checkbox each. In the load event of the page I add a click handler to every checkbox using this javascript code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  $$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.ConfermaInvitati'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Event.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span>element<span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> clickHandler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// ... </span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> clickHandler<span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> e <span style="color: #339933;">=</span> Event.<span style="color: #660066;">element</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #660066;">Updater</span><span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">up</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'my/invited/toggle'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    parameters<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> id<span style="color: #339933;">:</span> e.<span style="color: #660066;">up</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">up</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">id</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    onLoading<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> e.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;/images/admin/spinner.gif&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    onSuccess<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #660066;">Request</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'my/refresh'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span></pre></div></div>

	<p>This works fine until I add a new person via an Ajax call. That person won&#8217;t have a <em>clickHandler</em> because the element wasn&#8217;t on the page when I called the click handler. So I thought it was time to test <em>page.call</em> in the  <em>render :update</em> block I had in the rails application.</p>
	<p>I tried this code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">render <span style="color:#ff3333; font-weight:bold;">:update</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>page<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#008000; font-style:italic;"># Do stuff that creates the new objects and adds it to the page</span>
  <span style="color:#008000; font-style:italic;"># The data I need is in @invited</span>
  page.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Event.observe($$('##{@invited.permalink} .ConfermaInvitati').first()), 'click', clickHandler)&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

	<p>Obviously that didn&#8217;t work, and it turned out I have to read documentation before doing fancy things <img src='http://tempe.st/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
	<p>The rails docs told me that I had to use <em>call</em> passing the function name as the first argument and an array of parameters as the second argument, the problem is that <em>call</em> turns all the parameters into strings&#8212;this means I could not pass the <em>clickHandler</em> function to <em>Event.observe</em>. </p>
	<p>I found the solution in <em><<</em>. If you do <em>page << "foo"</em>, foo will be evaluated as raw javascripts. This meant I was able to do</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;Event.observe($$('##{@invited.permalink} .ConfermaInvitati').first()), 'click', clickHandler)</span></pre></div></div>

	<p>and finally have the functionality I was looking for. So remember, don&#8217;t <em>page.call</em> if you need to pass javascript variables to your functions.</p>

 ]]></content:encoded>
			<wfw:commentRss>http://tempe.st/2007/08/rails-and-javascript-pagecall-gotchas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The battle of the languages part II</title>
		<link>http://tempe.st/2007/05/the-battle-of-the-languages-part-ii/</link>
		<comments>http://tempe.st/2007/05/the-battle-of-the-languages-part-ii/#comments</comments>
		<pubDate>Wed, 23 May 2007 15:41:21 +0000</pubDate>
		<dc:creator>Giovanni Intini</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://tempe.st/2007/05/the-battle-of-the-languages-part-ii/</guid>
		<description><![CDATA[My previous post about a programming contest I had with my friends was wildly popular. Much much more than I even thought possible. I started with a simple comparison of PHP, C, Ruby, Erlang and Javascript and ended up with lots of comments where people posted interesting implementations in more and more languages, even obscure [...]]]></description>
			<content:encoded><![CDATA[	<p><a href="http://tempe.st/2007/05/erlang-ruby-and-php-battle-it-out">My previous post</a> about a programming contest I had with my friends was wildly popular. Much much more than I even thought possible.</p>
	<p>I started with a simple comparison of PHP, C, Ruby, Erlang and Javascript and ended up with lots of comments where people posted interesting implementations in more and more languages, even obscure ones (to me) like Haskell and Ocaml.</p>
	<p>I owe everyone a fairer benchmark so I will do the test again, this time testing the following languages:</p>
	<ul>
		<li>C</li>
		<li>Common Lisp</li>
		<li>Erlang</li>
		<li>Haskell</li>
		<li>Javascript</li>
		<li>Java</li>
		<li>Ocaml</li>
		<li>Perl</li>
		<li>PHP</li>
		<li>Python</li>
		<li>Ruby</li>
	</ul>
	<p>I need submissions for most of these languages, and I actually am quite open to submissions for the languages I speak <img src='http://tempe.st/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
	<p>The rules are simple, the program has to store the results in an array (not just count them) and has to run without any strange libraries or compilers or compiler switches. To avoid interpreter starting time problems, please submit programs that autotime themselves.</p>
	<p>As usual we&#8217;ll find the pythagorean triplets up to 5000. The reference implementation is the following, in Ruby:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">t = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
result = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006666;">2</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5000</span><span style="color:#006600; font-weight:bold;">&#41;</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:#006666;">1</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>c<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</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>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> t</pre></div></div>

	<p>This code takes 15.70s to run on my macbook pro. </p>
	<p>Now it&#8217;s your turn!</p>
	<p><strong>Update:</strong> Lua version by Sindisil</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #b1b100;">local</span> tt <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">os.clock</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">local</span> result <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
<span style="color: #b1b100;">local</span> c
  <span style="color: #b1b100;">for</span> c <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">5000</span> <span style="color: #b1b100;">do</span>
    <span style="color: #b1b100;">local</span> b
    <span style="color: #b1b100;">for</span> b <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>, c-<span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">do</span>
      <span style="color: #b1b100;">local</span> a <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">math.sqrt</span><span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">*</span>c - b<span style="color: #66cc66;">*</span>b<span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">if</span> a <span style="color: #66cc66;">==</span> <span style="color: #b1b100;">math.floor</span><span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">then</span>
      result<span style="color: #66cc66;">&#91;</span>#result+<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> a, b, c <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">end</span>
  <span style="color: #b1b100;">end</span>
<span style="color: #b1b100;">end</span>
<span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">os.clock</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> - tt<span style="color: #66cc66;">&#41;</span></pre></div></div>

	<p>It takes 5.48s to run on my box.</p>
	<p><strong>Update:</strong> new Ruby version.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#CC00FF; font-weight:bold;">Math</span>
&nbsp;
a, b, c = <span style="color:#006666;">0</span>
result = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006666;">2</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5000</span><span style="color:#006600; font-weight:bold;">&#41;</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> 
  d = c <span style="color:#006600; font-weight:bold;">*</span> c
  <span style="color:#006666;">1</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>c<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</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 = sqrt<span style="color:#006600; font-weight:bold;">&#40;</span>d <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;">floor</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;">floor</span> == a 
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#CC00FF; font-weight:bold;">Process</span>.<span style="color:#9900CC;">times</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

	<p>Following Gabriele Renzi&#8217;s suggestions we now have a faster ruby version: 12.30s</p>
	<p><strong>Update:</strong> faster Lua version from Chris Swetenham, it runs in 3.44s</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #b1b100;">local</span> tt <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">os.clock</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">local</span> result <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
<span style="color: #b1b100;">local</span> c
<span style="color: #b1b100;">local</span> n <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>
<span style="color: #b1b100;">local</span> <span style="color: #b1b100;">math</span> <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">math</span>
<span style="color: #b1b100;">local</span> <span style="color: #b1b100;">sqrt</span> <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">math.sqrt</span>
<span style="color: #b1b100;">local</span> <span style="color: #b1b100;">floor</span> <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">math.floor</span>
&nbsp;
<span style="color: #b1b100;">for</span> c <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">5000</span> <span style="color: #b1b100;">do</span>
  <span style="color: #b1b100;">local</span> b
  <span style="color: #b1b100;">for</span> b <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>, c-<span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">do</span>
    <span style="color: #b1b100;">local</span> a <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">sqrt</span><span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">*</span>c - b<span style="color: #66cc66;">*</span>b<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">if</span> a <span style="color: #66cc66;">==</span> <span style="color: #b1b100;">floor</span><span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">then</span>
      result<span style="color: #66cc66;">&#91;</span>n<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> a, b, c <span style="color: #66cc66;">&#125;</span>
      n <span style="color: #66cc66;">=</span> n + <span style="color: #cc66cc;">1</span>
    <span style="color: #b1b100;">end</span>
  <span style="color: #b1b100;">end</span>
<span style="color: #b1b100;">end</span>
&nbsp;
<span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">os.clock</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> - tt<span style="color: #66cc66;">&#41;</span></pre></div></div>

	<p><strong>Update:</strong> PHP version by <a href="http://fullo.net">Fullo</a></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;">$start</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span> 
       <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$b</span><span style="color: #339933;">,</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>              
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$start</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>It runs in 9.73s.</p>
	<p><strong>Update:</strong> Python version from ludo, it runs in 11.2s</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">time</span> <span style="color: #ff7700;font-weight:bold;">import</span> clock
&nbsp;
start = clock<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">math</span> <span style="color: #ff7700;font-weight:bold;">import</span> sqrt
&nbsp;
results = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">5001</span><span style="color: black;">&#41;</span>:
  d = c <span style="color: #66cc66;">*</span> c
  <span style="color: #ff7700;font-weight:bold;">for</span> b <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, c<span style="color: black;">&#41;</span>:
    a = sqrt<span style="color: black;">&#40;</span>d - b <span style="color: #66cc66;">*</span> b<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> a <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">1</span> == <span style="color: #ff4500;">0.0</span>:
      results.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>a, b, c<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> clock<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - start</pre></div></div>

	<p>The results for now:</p>
	<ol>
		<li>Lua 3.44</li>
		<li>PHP 9.73</li>
		<li>Python 11.20</li>
		<li>Ruby 12.30</li>
	</ol>


 ]]></content:encoded>
			<wfw:commentRss>http://tempe.st/2007/05/the-battle-of-the-languages-part-ii/feed/</wfw:commentRss>
		<slash:comments>31</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>A praise for jQuery</title>
		<link>http://tempe.st/2007/05/a-praise-for-jquery/</link>
		<comments>http://tempe.st/2007/05/a-praise-for-jquery/#comments</comments>
		<pubDate>Thu, 17 May 2007 07:55:17 +0000</pubDate>
		<dc:creator>Giovanni Intini</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://tempe.st/2007/05/a-praise-for-jquery/</guid>
		<description><![CDATA[I&#8217;ve been doing some drupal development lately, and had to make the switch from Prototype to jQuery (by the way it&#8217;s the same switch WordPress made in 2.2). At first I had to learn the different approach jQuery has to manipulating the DOM, but once I got the hang of it I really started appreciating [...]]]></description>
			<content:encoded><![CDATA[	<p>I&#8217;ve been doing some drupal development lately, and had to make the switch from <a href="http://prototypejs.org">Prototype</a> to <a href="http://jquery.com">jQuery</a> (by the way it&#8217;s the same switch WordPress made in 2.2).</p>
	<p>At first I had to learn the different approach jQuery has to manipulating the DOM, but once I got the hang of it I really started appreciating jQuery.</p>
	<p>The main difference I&#8217;ve found is that I prefer programming Prototype via the Rails helpers, while programming jQuery is so fun I prefer to do it directly in JavaScript.</p>
	<p>Actually it turned out so fun that I keep adding content to pages and changes links in JS, while I should do that in the backend.</p>
	<p>Here&#8217;s an example, from a drupal module I wrote:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#comments h2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">after</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;slider&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;slide-open&quot;&gt;Espandi tutti i commenti&lt;/a&gt; - &lt;a href=&quot;#&quot; class=&quot;slide-close&quot;&gt;Contrai tutti i commenti&lt;/a&gt;&lt;/p&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.comment .expand'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;[+]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.comment .content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.comment h3'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">siblings</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideToggle</span><span style="color: #009900;">&#40;</span><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>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.expand'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;[-]&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.expand'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;[+]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.expand'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;[-]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> 
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#comments .slider .slide-open'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.comment .expand'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;[-]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#comments .slider .slide-close'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.comment .expand'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;[+]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

	<p>What does this code do? Add a couple of links after the comments heading, hide all the comments and add a slidedown/up functionality to them.</p>
	<p>The best thing of adding this functionality in the frontend is that when the user has js disabled or is using a non-js browser the site falls back to the normal behavior.</p>

 ]]></content:encoded>
			<wfw:commentRss>http://tempe.st/2007/05/a-praise-for-jquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Javascript Classes</title>
		<link>http://tempe.st/2007/05/javascript-classes/</link>
		<comments>http://tempe.st/2007/05/javascript-classes/#comments</comments>
		<pubDate>Wed, 16 May 2007 09:37:15 +0000</pubDate>
		<dc:creator>Giovanni Intini</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Yahoo]]></category>

		<guid isPermaLink="false">http://tempe.st/2007/05/javascript-classes/</guid>
		<description><![CDATA[Before you go &#8220;hey, but js doesn&#8217;t have classes&#8221; let me tell I meant classes as in &#8220;school classes&#8221; I don&#8217;t usually like repeating myself and writing posts about links I put on my del.icio.us, but this time I have to. I&#8217;m having a great time learning from these videos of Doug Crockford&#8217;s presentation of [...]]]></description>
			<content:encoded><![CDATA[	<p>Before you go &#8220;hey, but js doesn&#8217;t have classes&#8221; let me tell I meant classes as in &#8220;school classes&#8221; <img src='http://tempe.st/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
	<p>I don&#8217;t usually like repeating myself and writing posts about links I put on <a href="http://del.icio.us/intinig">my del.icio.us</a>, but this time I have to. I&#8217;m having a great time learning from <a href="http://101out.com/js.php">these videos</a> of Doug Crockford&#8217;s presentation of JS @ Yahoo!</p>
	<p>Wonderful stuff there, after 15 minutes of the first video I already learnt some JS tricks I didn&#8217;t know (or didn&#8217;t care about).</p>

 ]]></content:encoded>
			<wfw:commentRss>http://tempe.st/2007/05/javascript-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

