<?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>Marco Mastrodonato</title>
	<atom:link href="http://mastrodonato.info/index.php/feed/?lang=en" rel="self" type="application/rss+xml" />
	<link>http://mastrodonato.info?lang=en</link>
	<description>Non c&#039;e&#039; prezzo per la miticita&#039;</description>
	<lastBuildDate>Wed, 08 Sep 2010 14:27:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Pluralize in every languages</title>
		<link>http://mastrodonato.info/index.php/2010/09/pluralize-in-every-languages/?lang=en</link>
		<comments>http://mastrodonato.info/index.php/2010/09/pluralize-in-every-languages/?lang=en#comments</comments>
		<pubDate>Wed, 08 Sep 2010 12:20:03 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby on Rails @en]]></category>
		<category><![CDATA[I18n]]></category>
		<category><![CDATA[Internationalization]]></category>
		<category><![CDATA[Pluralize]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=620</guid>
		<description><![CDATA[This article show you how to use that handy helper rails, pluralize with all the languages through the use of I18n. We assume that the application is internationalized, so model and attributes are in YAML configuration file. Now we add the terms for which we want to internationalize the plurality: models, attributes etc.. activerecord: &#38;activerecord [...]]]></description>
			<content:encoded><![CDATA[<p>This article show you how to use that handy helper rails, <strong> pluralize </strong> with all the languages through the use of I18n.</p>
<p>We assume that the application is internationalized, so model and attributes are in  YAML configuration file.<br />
Now we add the terms for which we want to internationalize the plurality: models, attributes etc..</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  activerecord: <span style="color:#006600; font-weight:bold;">&amp;</span>activerecord
    models: <span style="color:#006600; font-weight:bold;">&amp;</span>models
      user: <span style="color:#996600;">&quot;User&quot;</span>
      users: <span style="color:#996600;">&quot;Users&quot;</span>
      activity: <span style="color:#996600;">&quot;Activity&quot;</span>
      activities: <span style="color:#996600;">&quot;Activities&quot;</span>
      task: <span style="color:#996600;">&quot;Task&quot;</span>
      tasks: <span style="color:#996600;">&quot;Tasks&quot;</span>
      project: <span style="color:#996600;">&quot;Project&quot;</span>
      projects: <span style="color:#996600;">&quot;Projects&quot;</span>
&nbsp;
    attributes: <span style="color:#006600; font-weight:bold;">&amp;</span>attributes
      activity:
        task: <span style="color:#996600;">&quot;Task&quot;</span>
        day: <span style="color:#996600;">&quot;Day&quot;</span>
        hours: <span style="color:#996600;">&quot;Hours&quot;</span>
        hour: <span style="color:#996600;">&quot;Hour&quot;</span>
        description: <span style="color:#996600;">&quot;Description&quot;</span></pre></div></div>

<p>Assume that our model &#8220;Task&#8221; is related with the model &#8220;activity&#8221; which has a field named &#8220;Hours&#8221;. We want to create a phrase that describes how many activities there are for that task and the activity total hours.</p>
<p>According to data in the database, we get sentences like:<br />
<em>This task has <strong>1 activity</strong> with an amount of <strong>1 hour</strong>.<br />
This task has <strong>13 activities</strong> with an amount of <strong>0 hours</strong>.<br />
This task has <strong>1 activity</strong> with an amount of <strong>15 hours</strong>.</em></p>
<p>&#8230;and for all others languages:<br />
Questo incarico ha <strong>1 attività</strong> per un totale di <strong>15 ore</strong>.<br />
Diese Aufgabe haben <strong>1-Aktivität</strong> für eine Gesamtmenge von <strong>15 Stunden</strong>.<br />
Cette tâche a <strong>1 activité</strong> pour un total de <strong>15 heures</strong>.<br />
Esta tarea tiene <strong>1 actividad</strong> para un total de <strong>15 horas</strong>.<br />
このタスクは、 15時間の量1活性を持つ</p>
<p>We would to avoid:<br />
<del>This task has 1 activities with an amount of 1 hours</del><br />
<del>Questo incarico ha 1 impieghi per un totale di 1 ore</del><br />
etc.</p>
<p>Add the phrase in each file YAML I18n:</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">  #en.yml
  task_activities: &quot;This task has &lt;strong&gt;{{activities}}&lt;/strong&gt; with an amount of &lt;strong&gt;{{hours}}&lt;/strong&gt;.&quot;
  #it.yml
  task_activities: &quot;Questo incarico ha &lt;strong&gt;{{activities}}&lt;/strong&gt; per un totale di &lt;strong&gt;{{hours}}&lt;/strong&gt;.&quot;
  #etc.</pre></div></div>

<p>To do this, we&#8217;ll use the handy helper rails, pluralize, which will be amended to delete the number from the result. We&#8217;ll pluralize the English word to use as a key to I18n.</p>
<p>We create into application helper:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ApplicationHelper
   <span style="color:#008000; font-style:italic;">#Pluralize without the number</span>
   <span style="color:#9966CC; font-weight:bold;">def</span> I18n_pluralize<span style="color:#006600; font-weight:bold;">&#40;</span>count, singular, plural = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
     <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>count == <span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">||</span> count =~ <span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#40;</span>\.0<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>?$<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? singular : <span style="color:#006600; font-weight:bold;">&#40;</span>plural <span style="color:#006600; font-weight:bold;">||</span> singular.<span style="color:#9900CC;">pluralize</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>From the view, we translate the  phrase with I18n passing the number of activities and total hours (for description reasons, I put into the view some operations which would consider more appropriate place in the controllers):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#006600; font-weight:bold;">&lt;%</span> tot_hours = <span style="color:#0066ff; font-weight:bold;">@activities</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span> a.<span style="color:#9900CC;">hours</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>tot,h<span style="color:#006600; font-weight:bold;">|</span> tot<span style="color:#006600; font-weight:bold;">+</span>h<span style="color:#006600; font-weight:bold;">&#125;</span>  <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006666;">0</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span> str_hour = t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;attributes.activity.#{I18n_pluralize(tot_hours,'hour')}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span> str_activity = t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;models.#{I18n_pluralize(@activities.size,'activity')}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= t <span style="color:#ff3333; font-weight:bold;">:task_activities</span>, <span style="color:#ff3333; font-weight:bold;">:activities</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;#{@activities.size} #{str_activity}&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:hours</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;#{@tot_hours} #{str_hour}&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Observe this line in slow motion:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;attributes.activity.#{I18n_pluralize(tot_hours,'hour')}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>According on the hours number we get the key I18n: hour or hours</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">chiave = I18n_pluralize<span style="color:#006600; font-weight:bold;">&#40;</span>tot_hours,<span style="color:#996600;">'hour'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>equal to</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;attributes.activity.#{chiave}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>if tot_hours = 1 then:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;attributes.activity.hour&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>else:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;attributes.activity.hours&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>This is an outcome:<br />
<a href="http://mastrodonato.info/wp-content/uploads/2010/09/Esempio_Pluralize_I18n.png"><img src="http://mastrodonato.info/wp-content/uploads/2010/09/Esempio_Pluralize_I18n-300x248.png" alt="Example Pluralize I18n" title="Example Pluralize I18n" width="300" height="248" class="aligncenter size-medium wp-image-614" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2010/09/pluralize-in-every-languages/feed/?lang=en</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby 1.9.2 great performances</title>
		<link>http://mastrodonato.info/index.php/2010/09/ruby-1-9-2-great-performances/?lang=en</link>
		<comments>http://mastrodonato.info/index.php/2010/09/ruby-1-9-2-great-performances/?lang=en#comments</comments>
		<pubDate>Mon, 06 Sep 2010 21:01:22 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[JRuby @en]]></category>
		<category><![CDATA[Ruby @en]]></category>
		<category><![CDATA[jRuby]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[RVM]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=607</guid>
		<description><![CDATA[With a previous test, I found that JRuby was the highest expression in regard to the speed in executing ruby code, outperforming not only the C + + and .Net but also comparison with the last two incarnations of python. These are the scripts. Now that Rails 3 is released and an interpreter suggested is [...]]]></description>
			<content:encoded><![CDATA[<p>With a <a href="http://mastrodonato.info/index.php/2010/03/ruby-vs-python-vs-windows-vs-linux-2/?lang=en">previous test</a>, I found that JRuby was the highest expression in regard to the speed in executing ruby code, outperforming not only the C + + and .Net but also comparison with the last two incarnations of python. These are the <a href="http://mastrodonato.info/index.php/2010/01/ruby-vs-ruby-vs-python-vs-python-2/?lang=en">scripts</a>.</p>
<p>Now that Rails 3 is released and an interpreter suggested is the new 1.9.2, I was curious to observe how the official release works: well, I would say not bad!<br />
This time I limited the number of participants alone versions installed with <a href="http://rvm.beginrescueend.com/">RVM</a> and a couple more versions of Windows: JRuby 1.4.0 and the most used 1.8.7 mingw32, just to have a benchmark.</p>
<p>Linux distribution: Ubuntu 10.4 64bit<br />
Windows XP 32Bit SP3</p>
<p><center></p>
<table>
<th>Version</th>
<th>Builder/System</th>
<th>Seconds</th>
<tbody>
<tr>
<td>Ruby 1.9.2 p0 RVM</td>
<td>x86_64-linux</td>
<td><strong>6,6</strong></td>
</tr>
<tr>
<td>JRuby 1.4.0 RVM</td>
<td>OpenJDK 64-Bit Server VM 1.6.0_18 [amd64-java]</td>
<td>6,8</td>
</tr>
<tr>
<td>JRuby 1.5.2 RVM</td>
<td>OpenJDK 64-Bit Server VM 1.6.0_18 [amd64-java]</td>
<td>7,0</td>
</tr>
<tr>
<td>JRuby 1.4.0 </td>
<td>Windows Client VM 1.6.0_15 [x86-java]</td>
<td>7,0</td>
</tr>
<tr>
<td>Ruby 1.9.1 p378 RVM</td>
<td>x86_64-linux</td>
<td>8,9</td>
</tr>
<tr>
<td>Ruby 1.8.7 p249 RVM</td>
<td>x86_64-linux</td>
<td>12,2</td>
</tr>
<tr>
<td>Ruby 1.8.7 p249</td>
<td>i386-mingw32</td>
<td>23,9</td>
</tr>
</tbody>
</table>
<p></center></p>
<p><a href="http://mastrodonato.info/wp-content/uploads/2010/09/BenchMM1_20100906.png"><img src="http://mastrodonato.info/wp-content/uploads/2010/09/BenchMM1_20100906-300x150.png" alt="" title="BenchMM1 06/09/2010" width="300" height="150" class="aligncenter size-medium wp-image-596" /></a></p>
<p>This is the complete log of the execution:</p>
<p><code><br />
marco@d9400:~$ rvm list</p>
<p>rvm rubies</p>
<p>=> jruby-1.4.0 [ amd64-java ]<br />
   jruby-1.5.2 [ amd64-java ]<br />
   ruby-1.8.7-p249 [ x86_64 ]<br />
   ruby-1.9.1-p378 [ x86_64 ]<br />
   ruby-1.9.2-p0 [ x86_64 ]</p>
<p>marco@d9400:~$ java -version<br />
java version "1.6.0_18"<br />
OpenJDK Runtime Environment (IcedTea6 1.8.1) (6b18-1.8.1-0ubuntu1)<br />
OpenJDK 64-Bit Server VM (build 16.0-b13, mixed mode)</p>
<p>marco@d9400:~$ ruby -v<br />
jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (OpenJDK 64-Bit Server VM 1.6.0_18) [amd64-java]<br />
marco@d9400:~$ ruby RubyMM186_3.rb --server --fast</p>
<p>Warming up...<br />
Strings test - Elapsed 2.096<br />
Check1: 50000<br />
Check2: 157490<br />
Check3: 50005<br />
Check4: 373847<br />
Arrays test  - Elapsed 3.508<br />
Check1: 20900<br />
Check2: 250000<br />
Check3: 250000<br />
Check4: 1500000<br />
Check5: 5749950<br />
Numeric test - Elapsed 2.856<br />
Check1: 6252500<br />
Check2: 1439295494700374021157505910939096377494040420940312<br />
Ruby Partial elapsed time 8.460</p>
<p>1. Starting Ruby tests...<br />
Strings test - Elapsed 1.501<br />
Arrays test  - Elapsed 3.054<br />
Numeric test - Elapsed 2.280<br />
Ruby Partial elapsed time 6.835</p>
<p>2. Starting Ruby tests...<br />
Strings test - Elapsed 1.431<br />
Arrays test  - Elapsed 3.091<br />
Numeric test - Elapsed 2.249<br />
Ruby Partial elapsed time 6.771</p>
<p>3. Starting Ruby tests...<br />
Strings test - Elapsed 1.505<br />
Arrays test  - Elapsed 2.999<br />
Numeric test - Elapsed 2.261<br />
Ruby Partial elapsed time 6.765<br />
-------------------------------------<br />
Average Strings test - Elapsed 1.479<br />
Average Arrays test  - Elapsed 3.048<br />
Average Numeric test - Elapsed 2.263</p>
<p>Ruby Average elapsed time 6.790</p>
<p>marco@d9400:~$ rvm use jruby-1.5.2<br />
marco@d9400:~$ ruby -v<br />
jruby 1.5.2 (ruby 1.8.7 patchlevel 249) (2010-08-20 1c5e29d) (OpenJDK 64-Bit Server VM 1.6.0_18) [amd64-java]<br />
marco@d9400:~$ ruby RubyMM186_3.rb --server --fast</p>
<p>Warming up...<br />
Strings test - Elapsed 2.383<br />
Check1: 50000<br />
Check2: 157490<br />
Check3: 50005<br />
Check4: 373847<br />
Arrays test  - Elapsed 3.561<br />
Check1: 20900<br />
Check2: 250000<br />
Check3: 250000<br />
Check4: 1500000<br />
Check5: 5749950<br />
Numeric test - Elapsed 3.210<br />
Check1: 6252500<br />
Check2: 1439295494700374021157505910939096377494040420940312<br />
Ruby Partial elapsed time 9.154</p>
<p>1. Starting Ruby tests...<br />
Strings test - Elapsed 1.479<br />
Arrays test  - Elapsed 3.092<br />
Numeric test - Elapsed 2.516<br />
Ruby Partial elapsed time 7.087</p>
<p>2. Starting Ruby tests...<br />
Strings test - Elapsed 1.431<br />
Arrays test  - Elapsed 3.056<br />
Numeric test - Elapsed 2.508<br />
Ruby Partial elapsed time 6.995</p>
<p>3. Starting Ruby tests...<br />
Strings test - Elapsed 1.423<br />
Arrays test  - Elapsed 3.053<br />
Numeric test - Elapsed 2.524<br />
Ruby Partial elapsed time 7.000<br />
-------------------------------------<br />
Average Strings test - Elapsed 1.444<br />
Average Arrays test  - Elapsed 3.067<br />
Average Numeric test - Elapsed 2.516</p>
<p>Ruby Average elapsed time 7.027</p>
<p>marco@d9400:~$ rvm use ruby-1.8.7-p249<br />
marco@d9400:~$ ruby -v<br />
ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]<br />
marco@d9400:~$ ruby RubyMM186_3.rb</p>
<p>Warming up...<br />
Strings test - Elapsed 3.135<br />
Check1: 50000<br />
Check2: 157490<br />
Check3: 50005<br />
Check4: 373847<br />
Arrays test  - Elapsed 4.386<br />
Check1: 20900<br />
Check2: 250000<br />
Check3: 250000<br />
Check4: 1500000<br />
Check5: 5749950<br />
Numeric test - Elapsed 4.629<br />
Check1: 6252500<br />
Check2: 1439295494700374021157505910939096377494040420940312<br />
Ruby Partial elapsed time 12.149</p>
<p>1. Starting Ruby tests...<br />
Strings test - Elapsed 3.181<br />
Arrays test  - Elapsed 4.402<br />
Numeric test - Elapsed 4.598<br />
Ruby Partial elapsed time 12.180</p>
<p>2. Starting Ruby tests...<br />
Strings test - Elapsed 3.153<br />
Arrays test  - Elapsed 4.387<br />
Numeric test - Elapsed 4.572<br />
Ruby Partial elapsed time 12.113</p>
<p>3. Starting Ruby tests...<br />
Strings test - Elapsed 3.210<br />
Arrays test  - Elapsed 4.395<br />
Numeric test - Elapsed 4.583<br />
Ruby Partial elapsed time 12.189<br />
-------------------------------------<br />
Average Strings test - Elapsed 3.181<br />
Average Arrays test  - Elapsed 4.395<br />
Average Numeric test - Elapsed 4.584</p>
<p>Ruby Average elapsed time 12.160</p>
<p>marco@d9400:~$ rvm use ruby-1.9.1<br />
marco@d9400:~$ ruby -v<br />
ruby 1.9.1p378 (2010-01-10 revision 26273) [x86_64-linux]<br />
marco@d9400:~$ ruby RubyMM191_3.rb</p>
<p>Warming up...<br />
Strings test - Elapsed 2.452<br />
Check1: 50000<br />
Check2: 157490<br />
Check3: 50005<br />
Check4: 373847<br />
Arrays test  - Elapsed 4.488<br />
Check1: 20900<br />
Check2: 250000<br />
Check3: 250000<br />
Check4: 1500000<br />
Check5: 5749950<br />
Numeric test - Elapsed 1.840<br />
Check1: 6252500<br />
Check2: 1439295494700374021157505910939096377494040420940312<br />
Ruby Partial elapsed time 8.780</p>
<p>1. Starting Ruby tests...<br />
Strings test - Elapsed 2.314<br />
Arrays test  - Elapsed 4.679<br />
Numeric test - Elapsed 1.843<br />
Ruby Partial elapsed time 8.836</p>
<p>2. Starting Ruby tests...<br />
Strings test - Elapsed 2.319<br />
Arrays test  - Elapsed 4.691<br />
Numeric test - Elapsed 1.843<br />
Ruby Partial elapsed time 8.853</p>
<p>3. Starting Ruby tests...<br />
Strings test - Elapsed 2.318<br />
Arrays test  - Elapsed 4.705<br />
Numeric test - Elapsed 1.847<br />
Ruby Partial elapsed time 8.870<br />
-------------------------------------<br />
Average Strings test - Elapsed 2.317<br />
Average Arrays test  - Elapsed 4.692<br />
Average Numeric test - Elapsed 1.844</p>
<p>Ruby Average elapsed time 8.853</p>
<p>marco@d9400:~$ rvm use ruby-1.9.2<br />
marco@d9400:~$ ruby -v<br />
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]<br />
marco@d9400:~$ ruby RubyMM191_3.rb</p>
<p>Warming up...<br />
Strings test - Elapsed 1.680<br />
Check1: 50000<br />
Check2: 157490<br />
Check3: 50005<br />
Check4: 373847<br />
Arrays test  - Elapsed 4.192<br />
Check1: 20900<br />
Check2: 250000<br />
Check3: 250000<br />
Check4: 1500000<br />
Check5: 5749950<br />
Numeric test - Elapsed 1.708<br />
Check1: 6252500<br />
Check2: 1439295494700374021157505910939096377494040420940312<br />
Ruby Partial elapsed time 7.579</p>
<p>1. Starting Ruby tests...<br />
Strings test - Elapsed 1.654<br />
Arrays test  - Elapsed 3.178<br />
Numeric test - Elapsed 1.704<br />
Ruby Partial elapsed time 6.536</p>
<p>2. Starting Ruby tests...<br />
Strings test - Elapsed 1.746<br />
Arrays test  - Elapsed 3.388<br />
Numeric test - Elapsed 1.699<br />
Ruby Partial elapsed time 6.833</p>
<p>3. Starting Ruby tests...<br />
Strings test - Elapsed 1.644<br />
Arrays test  - Elapsed 3.227<br />
Numeric test - Elapsed 1.706<br />
Ruby Partial elapsed time 6.577<br />
-------------------------------------<br />
Average Strings test - Elapsed 1.681<br />
Average Arrays test  - Elapsed 3.264<br />
Average Numeric test - Elapsed 1.703</p>
<p>Ruby Average elapsed time 6.649<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2010/09/ruby-1-9-2-great-performances/feed/?lang=en</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create skeleton applications with generators and templates</title>
		<link>http://mastrodonato.info/index.php/2010/03/create-skeleton-applications-with-generators-and-templates/?lang=en</link>
		<comments>http://mastrodonato.info/index.php/2010/03/create-skeleton-applications-with-generators-and-templates/?lang=en#comments</comments>
		<pubDate>Fri, 19 Mar 2010 18:19:10 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby on Rails @en]]></category>
		<category><![CDATA[Generators]]></category>
		<category><![CDATA[nifty-generators]]></category>
		<category><![CDATA[Templates]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=453</guid>
		<description><![CDATA[Last update on April 1th, 2010 Rails is a wonderful framework which help us to develop a modern web application. However, when you create a new one, you should perform several pretty boring operations: add plugins / gems, rescue a starting layout and whatever you usually use in yours applications. To solve this, from version [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Last update on  April 1th, 2010</strong></p>
<p>Rails is a wonderful framework which help us to develop a modern web application. However, when you create a new one, you should perform several pretty boring operations: add plugins / gems, rescue a starting layout and whatever you usually use in yours applications.<br />
To solve this, from version 2.3 you may use templates. You find lots of examples on internet, i give you another that show you how to take advantage of it combining with a cool rails generator. <a href="http://railscasts.com/episodes/58-how-to-make-a-generator">This guide</a> explains how to make one. If you are impatient you can choose something already done, forking an existing project so did I. I customized Ryan Bates&#8217;s nifty-generators adding some new features:</p>
<ul>
<li>nifty_layout now supports multiple styles: classic, and blackwhite cloudy. It are only a starting point </li>
<li>nifty_layout: I added the configuration file config.rb which is a container for all the application constants for example application name, the default style etc.. </li>
<li>I introduced localization support: nifty_layout added YAML files for language en and it. To manage, some code is inserted into application controller: with the lang parameter you change the language setting (for example ?lang=en) that will be maintained throughout the session. The nifty_scaffold adds resources and its attributes, ready to be translated.</li>
<li>nifty_layout: the posts division has been moved into a partial in the shared folder. I preferred to do this to use it even through ajax </li>
<li> nifty_scaffold: the &#8220;new&#8221; and &#8220;edit&#8221; views now use two partial: render @ model which calls the second with the fields inside. So do the original version, it supports haml and sass, furthermore, I added support for formtastic. So you can choose the preferred combination!</li>
</ul>
<p></p>
<p>The template I will show you download these new generators for you, however, these are the <a href="http://github.com/marcomd/nifty-generators">sources</a> or you can even download latest <a href="http://github.com/marcomd/nifty-generators/downloads">gem and template</a>.</p>
<p>What the generator should do and what the template?<br />
Well, templates should be a container of things to do, executed only one time after creating a new project. Generators should create something like adding new files or customize something that already exists and can be executes whenever you want as well as within a template.</p>
<p>If you never have used a template before, you could start with a simple one or a tutorial could be even better:<br />
this <a href="http://railscasts.com/episodes/148-app-templates-in-rails-2-3">screencast</a> show you how to make a template.<br />
This is <a href="http://m.onkey.org/2008/12/4/rails-templates">an article</a> in html format.</p>
<p>I see many examples on internet which are filled with configuration&#8217;s parameters, that cause me strong headaches. I opted for one that ask me what i want. Surely, isn&#8217;t perfect yet but I think it&#8217;s a good base :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#This is a Rails 2.3 template</span>
<span style="color:#008000; font-style:italic;">#Written by Marco Mastrodonato, last update on 01/04/2010 </span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#008000; font-style:italic;"># USAGE: rails yourapp -m template.rb</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#This is the unique parameter, after first execution use FALSE to avoid installation</span>
install_gems = <span style="color:#0000FF; font-weight:bold;">false</span>
git_http = <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
WINDOWS = <span style="color:#006600; font-weight:bold;">&#40;</span>RUBY_PLATFORM =~ <span style="color:#006600; font-weight:bold;">/</span>dos<span style="color:#006600; font-weight:bold;">|</span>win32<span style="color:#006600; font-weight:bold;">|</span>cygwin<span style="color:#006600; font-weight:bold;">/</span>i<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006600; font-weight:bold;">&#40;</span>RUBY_PLATFORM =~ <span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>:?mswin<span style="color:#006600; font-weight:bold;">|</span>mingw<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span> 
&nbsp;
<span style="color:#008000; font-style:italic;">#plugin/install doesn't work on my windows systems</span>
<span style="color:#9966CC; font-weight:bold;">def</span> myplugin<span style="color:#006600; font-weight:bold;">&#40;</span>name, url<span style="color:#006600; font-weight:bold;">&#41;</span>
  url.<span style="color:#CC0066; font-weight:bold;">sub!</span> <span style="color:#996600;">'git://'</span>, <span style="color:#996600;">'http://'</span> <span style="color:#9966CC; font-weight:bold;">if</span> git_http
  <span style="color:#9966CC; font-weight:bold;">if</span> WINDOWS
    <span style="color:#008000; font-style:italic;">#run &quot;ruby script/plugin install #{url}&quot;</span>
    run <span style="color:#996600;">&quot;git clone #{url} vendor/plugins/#{name}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    plugin name, <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> url
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'*'</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">40</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;* Processing template#{WINDOWS ? ' on windows system': ''}...&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'*'</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">40</span>
&nbsp;
use_git = yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Do you think to use git ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> use_git
  git <span style="color:#ff3333; font-weight:bold;">:init</span>
  file <span style="color:#996600;">&quot;.gitignore&quot;</span>, <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^    <span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    .<span style="color:#9900CC;">DS_Store</span>
    log<span style="color:#006600; font-weight:bold;">/*</span>.<span style="color:#9900CC;">log</span>
    tmp<span style="color:#006600; font-weight:bold;">/**/*</span>
    config<span style="color:#006600; font-weight:bold;">/</span>database.<span style="color:#9900CC;">yml</span>
    db<span style="color:#006600; font-weight:bold;">/*</span>.<span style="color:#9900CC;">sqlite3</span>
    nbproject<span style="color:#006600; font-weight:bold;">/*</span>
  EOS
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#Everyone must to have it!</span>
plugin <span style="color:#996600;">'will_paginate'</span>,
        <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;git://github.com/mislav/will_paginate.git&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Will Paginate ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Attachments with no extra database tables, only one library to install for image processing</span>
plugin <span style="color:#996600;">'paperclip'</span>,
       <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;git://github.com/thoughtbot/paperclip.git&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Paperclip ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
formtastic = yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Formtastic ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> formtastic
  <span style="color:#008000; font-style:italic;">#gem 'justinfrench-formtastic', :lib =&gt; 'formtastic', :source =&gt; 'http://gems.github.com'</span>
  <span style="color:#008000; font-style:italic;">#rake &quot;gems:install&quot; if install_gem</span>
&nbsp;
  <span style="color:#008000; font-style:italic;">#A Rails FormBuilder DSL (with some other goodies) to make it far easier to create beautiful, semantically rich, syntactically awesome, readily stylable and wonderfully accessible HTML forms in your Rails applications.</span>
  plugin <span style="color:#996600;">'formtastic'</span>,
        <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;git://github.com/justinfrench/formtastic.git&quot;</span>
  <span style="color:#008000; font-style:italic;">#Adds reflective access to validations</span>
  plugin <span style="color:#996600;">'validation_reflection'</span>,
        <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;git://github.com/redinger/validation_reflection.git&quot;</span>
  generate <span style="color:#996600;">&quot;formtastic&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Add testing framework ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># RSpec's official Ruby on Rails plugin  </span>
  plugin <span style="color:#996600;">'rspec'</span>, 
    <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'git://github.com/dchelimsky/rspec.git'</span>
&nbsp;
  plugin <span style="color:#996600;">'rspec-rails'</span>,
         <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;git://github.com/dchelimsky/rspec-rails.git&quot;</span>
  generate <span style="color:#996600;">&quot;rspec&quot;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># BDD that talks to domain experts first and code 2nd</span>
  plugin <span style="color:#996600;">'cucumber'</span>,
         <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;git://github.com/aslakhellesoy/cucumber.git&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Add authentication ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;0. None&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;1. Devise/warden&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;2. Authlogic&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;3. Restful authentication&quot;</span>
  choose = ask<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Choose one:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">case</span> choose
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;1&quot;</span>
      gem <span style="color:#996600;">&quot;warden&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;0.9.6&quot;</span>
      gem <span style="color:#996600;">&quot;devise&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;1.0.4&quot;</span>
      rake <span style="color:#996600;">&quot;gems:install&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> install_gems
      generate <span style="color:#996600;">&quot;devise_install&quot;</span>
      generate <span style="color:#996600;">&quot;devise&quot;</span>, <span style="color:#996600;">&quot;User&quot;</span>
      generate <span style="color:#996600;">&quot;devise_views&quot;</span>
      <span style="color:#008000; font-style:italic;">#User.create!(:email =&gt; 'admin@administrator.com', :password =&gt; 'secret')</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;2&quot;</span>
      gem <span style="color:#996600;">&quot;authlogic&quot;</span>
      rake <span style="color:#996600;">&quot;gems:install&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> install_gems
      generate <span style="color:#996600;">&quot;session&quot;</span>, <span style="color:#996600;">&quot;user_session&quot;</span>
      generate <span style="color:#996600;">&quot;model&quot;</span>, <span style="color:#996600;">&quot;user&quot;</span>
      generate <span style="color:#996600;">&quot;controller&quot;</span>, <span style="color:#996600;">&quot;user_sessions&quot;</span>
      route <span style="color:#996600;">&quot;map.resource :user_session&quot;</span>
      create_users_file = <span style="color:#CC00FF; font-weight:bold;">Dir</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'db/migrate/*_create_users.rb'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">first</span>
      file create_users_file, <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^        <span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">class</span> CreateUsers <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
          <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
            create_table <span style="color:#ff3333; font-weight:bold;">:users</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:login</span>,               <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>                <span style="color:#008000; font-style:italic;"># optional, you can use email instead, or both</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:email</span>,               <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>                <span style="color:#008000; font-style:italic;"># optional, you can use login instead, or both</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:crypted_password</span>,    <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>                <span style="color:#008000; font-style:italic;"># optional, see below</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:password_salt</span>,       <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>                <span style="color:#008000; font-style:italic;"># optional, but highly recommended</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:persistence_token</span>,   <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>                <span style="color:#008000; font-style:italic;"># required</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:single_access_token</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>                <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::Params</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:perishable_token</span>,    <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>                <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::Perishability</span>
&nbsp;
              <span style="color:#008000; font-style:italic;"># Magic columns, just like ActiveRecord's created_at and updated_at. These are automatically maintained by Authlogic if they are present.</span>
              t.<span style="color:#CC0066; font-weight:bold;">integer</span>   <span style="color:#ff3333; font-weight:bold;">:login_count</span>,         <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>, <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0</span> <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::MagicColumns</span>
              t.<span style="color:#CC0066; font-weight:bold;">integer</span>   <span style="color:#ff3333; font-weight:bold;">:failed_login_count</span>,  <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>, <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0</span> <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::MagicColumns</span>
              t.<span style="color:#9900CC;">datetime</span>  <span style="color:#ff3333; font-weight:bold;">:last_request_at</span>                                    <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::MagicColumns</span>
              t.<span style="color:#9900CC;">datetime</span>  <span style="color:#ff3333; font-weight:bold;">:current_login_at</span>                                   <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::MagicColumns</span>
              t.<span style="color:#9900CC;">datetime</span>  <span style="color:#ff3333; font-weight:bold;">:last_login_at</span>                                      <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::MagicColumns</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:current_login_ip</span>                                   <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::MagicColumns</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:last_login_ip</span>                                      <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::MagicColumns</span>
&nbsp;
              t.<span style="color:#9900CC;">timestamps</span>
            <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
            add_index <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:email</span>
            add_index <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:login</span>, <span style="color:#ff3333; font-weight:bold;">:unique</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
          <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
            drop_table <span style="color:#ff3333; font-weight:bold;">:users</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      EOS
      file <span style="color:#996600;">'app/models/user.rb'</span>, <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^        <span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
          acts_as_authentic
        <span style="color:#9966CC; font-weight:bold;">end</span>
      EOS
      file <span style="color:#996600;">'spec/models/user_spec.rb'</span>, <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^        <span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'spec_helper'</span>
&nbsp;
        describe User <span style="color:#9966CC; font-weight:bold;">do</span>
          before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
            <span style="color:#0066ff; font-weight:bold;">@valid_attributes</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>
              <span style="color:#ff3333; font-weight:bold;">:login</span>                 <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;login&quot;</span>,
              <span style="color:#ff3333; font-weight:bold;">:email</span>                 <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;some@thing.com&quot;</span>,
              <span style="color:#ff3333; font-weight:bold;">:password</span>              <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;password&quot;</span>,
              <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;password&quot;</span>
            <span style="color:#006600; font-weight:bold;">&#125;</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
          it <span style="color:#996600;">&quot;should create a new instance given valid attributes&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
            User.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>@valid_attributes<span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      EOS
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;3&quot;</span>
      plugin <span style="color:#996600;">'restful-authentication-i18n'</span>,
          <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;git://github.com/dcrec1/restful-authentication-i18n.git&quot;</span>
      <span style="color:#008000; font-style:italic;">#â€”include-activation \ â€”stateful \ â€”rspec \ â€”skip-migration \ â€”skip-routes \ â€”old-passwords </span>
      generate <span style="color:#996600;">&quot;authenticated&quot;</span>, <span style="color:#996600;">&quot;user&quot;</span>, <span style="color:#996600;">&quot;sessions&quot;</span>, <span style="color:#996600;">&quot;â€”include-activation&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Authorization ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    gem <span style="color:#996600;">&quot;cancan&quot;</span>
    rake <span style="color:#996600;">&quot;gems:install&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> install_gems
    <span style="color:#008000; font-style:italic;">#plugin 'cancan', :git =&gt; &quot;git://github.com/ryanb/cancan.git&quot; </span>
    file <span style="color:#996600;">'spec/models/ability.rb'</span>, <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^      <span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">class</span> Ability  
        <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">CanCan::Ability</span>  
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>user<span style="color:#006600; font-weight:bold;">&#41;</span>  
          user <span style="color:#006600; font-weight:bold;">||</span>= User.<span style="color:#9900CC;">new</span> <span style="color:#008000; font-style:italic;"># Guest user  </span>
          <span style="color:#9966CC; font-weight:bold;">if</span> user.<span style="color:#9900CC;">role</span>? <span style="color:#ff3333; font-weight:bold;">:admin</span>  
            can <span style="color:#ff3333; font-weight:bold;">:manage</span>, <span style="color:#ff3333; font-weight:bold;">:all</span>  
          <span style="color:#9966CC; font-weight:bold;">else</span>  
            can <span style="color:#ff3333; font-weight:bold;">:read</span>, <span style="color:#ff3333; font-weight:bold;">:all</span>  
          <span style="color:#9966CC; font-weight:bold;">end</span>  
        <span style="color:#9966CC; font-weight:bold;">end</span>  
      <span style="color:#9966CC; font-weight:bold;">end</span>  
    EOS
    generate <span style="color:#996600;">&quot;migration&quot;</span>, <span style="color:#996600;">&quot;add_roles_mask_to_users roles_mask:integer&quot;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Add these lines to user model:&quot;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^      <span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      named_scope <span style="color:#ff3333; font-weight:bold;">:with_role</span>, <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>role<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:conditions <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;roles_mask &amp; <span style="color:#000099;">\#</span>{2**ROLES.index(role.to_s)} &gt; 0 &quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> roles=<span style="color:#006600; font-weight:bold;">&#40;</span>roles<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">roles_mask</span> = <span style="color:#006600; font-weight:bold;">&#40;</span>roles <span style="color:#006600; font-weight:bold;">&amp;</span> ROLES<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>r<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">**</span>ROLES.<span style="color:#9900CC;">index</span><span style="color:#006600; font-weight:bold;">&#40;</span>r<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">sum</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> roles
        ROLES.<span style="color:#9900CC;">reject</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>r<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>roles_mask <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">**</span>ROLES.<span style="color:#9900CC;">index</span><span style="color:#006600; font-weight:bold;">&#40;</span>r<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">zero</span>? <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> role_symbols
        roles.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:to_sym<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    EOS
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>          
&nbsp;
haml = yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Haml?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> haml
  <span style="color:#008000; font-style:italic;"># The world's greatest templating system</span>
  <span style="color:#008000; font-style:italic;">#plugin 'haml', :git =&gt; &quot;git://github.com/nex3/haml.git&quot;</span>
  gem <span style="color:#996600;">&quot;haml&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:lib</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;haml&quot;</span>
  rake <span style="color:#996600;">&quot;gems:install&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> install_gems
  run <span style="color:#996600;">&quot;haml --rails .&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Layout ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  plugin <span style="color:#996600;">&quot;marcomd-nifty-generators&quot;</span>, 
    <span style="color:#ff3333; font-weight:bold;">:lib</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;marcomd-nifty-generators&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;git://github.com/marcomd/nifty-generators.git&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot; 1. classic&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot; 2. cloudy&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot; 3. blackwhite&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;or. write names separed by space&quot;</span>
  style = ask<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Choose style:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  style = <span style="color:#9966CC; font-weight:bold;">case</span> style
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;1&quot;</span> <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#996600;">&quot;classic&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;2&quot;</span> <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#996600;">&quot;cloudy&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;3&quot;</span> <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#996600;">&quot;blackwhite&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span> style
  <span style="color:#9966CC; font-weight:bold;">end</span>
  generate <span style="color:#996600;">&quot;nifty_layout&quot;</span>, <span style="color:#996600;">&quot;application #{style} #{haml ? '--haml' : ''} #{formtastic ? '--formtastic' : ''}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Link to local copy of edge rails</span>
<span style="color:#008000; font-style:italic;">#inside('vendor') { run 'ln -s ~/dev/rails/rails rails' } if yes?(&quot;Rails ?&quot;)</span>
rake<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;rails:freeze:gems&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Freeze rails gems ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Mailer dummy config</span>
initializer <span style="color:#996600;">&quot;mailer.rb&quot;</span>, <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^  <span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  mailer_options = <span style="color:#CC00FF; font-weight:bold;">YAML</span>.<span style="color:#9900CC;">load_file</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\#</span>{Rails.root}/config/mailer.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#6666ff; font-weight:bold;">ActionMailer::Base</span>.<span style="color:#9900CC;">smtp_settings</span> = mailer_options
EOS
file <span style="color:#996600;">&quot;config/mailer.yml&quot;</span>, <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^  <span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#ff3333; font-weight:bold;">:address</span>: mail.<span style="color:#9900CC;">authsmtp</span>.<span style="color:#9900CC;">com</span>
  <span style="color:#ff3333; font-weight:bold;">:domain</span>: yourdomain.<span style="color:#9900CC;">com</span>
  <span style="color:#ff3333; font-weight:bold;">:authentication</span>: <span style="color:#ff3333; font-weight:bold;">:login</span>
  <span style="color:#ff3333; font-weight:bold;">:user_name</span>: USERNAME
  <span style="color:#ff3333; font-weight:bold;">:password</span>: PASSWORD
EOS
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Generate controller home ? (suggested)&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  generate <span style="color:#996600;">&quot;controller&quot;</span>, <span style="color:#996600;">&quot;home&quot;</span>, <span style="color:#996600;">&quot;index&quot;</span>
  route <span style="color:#996600;">&quot;map.root :controller =&gt; :home&quot;</span>
<span style="color:#9966CC; font-weight:bold;">else</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
rake <span style="color:#996600;">&quot;gems:install&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> install_gems
<span style="color:#008000; font-style:italic;"># Unpack all gems to vendor/gems</span>
rake <span style="color:#996600;">&quot;gems:unpack&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Unpack to vendor/gems ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
rake <span style="color:#996600;">&quot;db:create:all&quot;</span>
rake <span style="color:#996600;">&quot;db:migrate&quot;</span>
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">unlink</span> <span style="color:#996600;">&quot;public/index.html&quot;</span>
&nbsp;
git <span style="color:#ff3333; font-weight:bold;">:add</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;.&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:commit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;-m 'initial commit'&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> use_git
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;ENJOY!&quot;</span></pre></div></div>

<p><strong>This is a result:</strong></p>
<p><code><br />
marco@d9400:~/Rails$ rails Blog -m marcomd.rb<br />
      create  bla bla bla<br />
    applying  template: marcomd.rb<br />
Processing template...<br />
      plugin  will_paginate<br />
Unpacking objects: 100% (57/57), done.<br />
From git://github.com/mislav/will_paginate<br />
 * branch            HEAD       -> FETCH_HEAD<br />
              Paperclip ?<br />
y<br />
      plugin  paperclip<br />
Unpacking objects: 100% (78/78), done.<br />
From git://github.com/thoughtbot/paperclip<br />
 * branch            HEAD       -> FETCH_HEAD<br />
              Formtastic ?<br />
y<br />
         gem  justinfrench-formtastic<br />
  generating  formtastic_stylesheets<br />
              Add testing framework ?<br />
y<br />
      plugin  rspec<br />
From git://github.com/dchelimsky/rspec<br />
 * branch            HEAD       -> FETCH_HEAD<br />
      plugin  rspec-rails<br />
From git://github.com/dchelimsky/rspec-rails<br />
 * branch            HEAD       -> FETCH_HEAD<br />
      plugin  cucumber<br />
From git://github.com/aslakhellesoy/cucumber<br />
 * branch            HEAD       -> FETCH_HEAD<br />
              Add authentication ?<br />
y<br />
0. None<br />
1. Devise/warden<br />
2. Authlogic<br />
3. Restful authentication<br />
              Choose one:<br />
1<br />
         gem  warden<br />
         gem  devise<br />
  generating  devise_install<br />
              Authorization ?<br />
n<br />
              Haml?<br />
n<br />
              Layout ?<br />
y<br />
      plugin  marcomd-nifty-generators<br />
From git://github.com/marcomd/nifty-generators<br />
 * branch            HEAD       -> FETCH_HEAD<br />
 1. classic<br />
 2. cloudy<br />
 3. blackwhite<br />
or. write names separed by space<br />
              Choose style:<br />
2<br />
  generating  nifty_layout<br />
              Freeze rails gems ?<br />
n<br />
 initializer  mailer.rb<br />
        file  config/mailer.yml<br />
              Generate controller home ? (suggested)<br />
y<br />
  generating  controller<br />
       route  map.root :controller => :home<br />
              Unpack to vendor/gems ?<br />
n<br />
        rake  db:create:all<br />
        rake  db:migrate<br />
ENJOY!<br />
     applied  marcomd.rb<br />
</code></p>
<p>And here we are! Now let&#8217;s start up mongrel&#8230;</p>
<p><code><br />
marco@d9400:~/Rails$ cd Blog/<br />
marco@d9400:~/Rails/Blog$ script/server<br />
=> Booting Mongrel<br />
=> Rails 2.3.5 application starting on http://0.0.0.0:3000<br />
=> Call with -d to detach<br />
=> Ctrl-C to shutdown server<br />
</code></p>
<p>anche check our new blog<br />
<a href="http://mastrodonato.info/wp-content/uploads/2010/03/Blog_marcomd1.png"><img src="http://mastrodonato.info/wp-content/uploads/2010/03/Blog_marcomd1-300x107.png" alt="This is only the beginning" title="This is only the beginning" width="300" height="107" class="aligncenter size-medium wp-image-462" /></a></p>
<p>Now, let&#8217;s add the post resource and migrate:</p>
<p><code><br />
marco@d9400:~/Rails$ cd Blog/<br />
marco@d9400:~/Rails/Blog$ script/generate nifty_scaffold post title:string body:text<br />
      exists  app/models<br />
      create  app/models/post.rb<br />
      exists  db/migrate<br />
      create  db/migrate/20100319175545_create_posts.rb<br />
      exists  test/unit<br />
      create  test/unit/post_test.rb<br />
      exists  test/fixtures<br />
      create  test/fixtures/posts.yml<br />
      exists  app/controllers<br />
      create  app/controllers/posts_controller.rb<br />
      exists  app/helpers<br />
      create  app/helpers/posts_helper.rb<br />
      create  app/views/posts<br />
      create  app/views/posts/index.html.erb<br />
      create  app/views/posts/show.html.erb<br />
      create  app/views/posts/new.html.erb<br />
      create  app/views/posts/edit.html.erb<br />
      create  app/views/posts/_post.html.erb<br />
      create  app/views/posts/_fields.html.erb<br />
       route  map.resources :posts<br />
      exists  test/functional<br />
      create  test/functional/posts_controller_test.rb<br />
marco@d9400:~/Rails/Blog$ rake db:migrate<br />
(in /home/marco/Rails/Blog)<br />
==  CreatePosts: migrating ====================================================<br />
-- create_table(:posts)<br />
   -> 0.0016s<br />
==  CreatePosts: migrated (0.0017s) ===========================================<br />
</code></p>
<p><a href="http://mastrodonato.info/wp-content/uploads/2010/03/Blog_marcomd2.png"><img src="http://mastrodonato.info/wp-content/uploads/2010/03/Blog_marcomd2-300x293.png" alt="Add post resource" title="Add post resource" width="300" height="293" class="aligncenter size-medium wp-image-465" /></a></p>
<p>Controllers and views uses I18n messages, for this reason, the new resource must be add to localization files. Nifty_scaffold will do it for you  :</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">#config/locales/en.yml
en:
  activerecord:
    models: &amp;models
      post: &quot;Post&quot;
      posts: &quot;Posts&quot;
    #DO NOT REMOVE MODELS
    attributes: &amp;attributes
      title: &quot;Title&quot;
      body: &quot;Body&quot;
    #DO NOT REMOVE ATTRIBUTES
&nbsp;
  formtastic:
    titles:
    labels:
    hints:
      post:
        title: &quot;Choose a good title for your article&quot;
        body: &quot;Choose a good body for your article&quot;
    #DO NOT REMOVE HINTS
    actions: &amp;actions
      create: &quot;Create my {{model}}&quot;
      update: &quot;Save changes&quot;
  &lt;&lt;: *models
  &lt;&lt;: *attributes
  &lt;&lt;: *actions
&nbsp;
#config/locales/it.yml
it:
  activerecord:
    models: &amp;models
      post: &quot;Messaggio&quot;
      posts: &quot;Messaggi&quot;
    #DO NOT REMOVE MODELS
    attributes: &amp;attributes
      title: &quot;Titolo&quot;
      body: &quot;Corpo&quot;
    #DO NOT REMOVE ATTRIBUTES
&nbsp;
  formtastic:
    titles:
    labels:
    hints:
      post:
        title: &quot;Choose a good title for your article&quot;
        body: &quot;Choose a good body for your article&quot;
    #DO NOT REMOVE HINTS
    actions: &amp;actions
      create: &quot;Crea {{model}}&quot;
      update: &quot;Salva le modifiche&quot;
  #DO NOT REMOVE FORMTASTIC
  &lt;&lt;: *models
  &lt;&lt;: *attributes
  &lt;&lt;: *actions</pre></div></div>

<p>I have to use tag <em>#DO NOT REMOVE ecc.</em> to put attributes and resource name inside so please don&#8217;t remove it.</p>
<p><a href="http://mastrodonato.info/wp-content/uploads/2010/03/Blog_marcomd3.png"><img src="http://mastrodonato.info/wp-content/uploads/2010/03/Blog_marcomd3-300x156.png" alt="Post created sucessfully" title="Post created sucessfully" width="300" height="156" class="aligncenter size-medium wp-image-469" /></a></p>
<p>I recently began testing Haml and Sass version for which there may be still some bugs.<br />
I hope this can be helpful, enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2010/03/create-skeleton-applications-with-generators-and-templates/feed/?lang=en</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby vs Python vs Windows vs Linux</title>
		<link>http://mastrodonato.info/index.php/2010/03/ruby-vs-python-vs-windows-vs-linux-2/?lang=en</link>
		<comments>http://mastrodonato.info/index.php/2010/03/ruby-vs-python-vs-windows-vs-linux-2/?lang=en#comments</comments>
		<pubDate>Sat, 06 Mar 2010 10:47:03 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[IronRuby .NET @en]]></category>
		<category><![CDATA[JRuby @en]]></category>
		<category><![CDATA[Ruby @en]]></category>
		<category><![CDATA[Benchmark]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=441</guid>
		<description><![CDATA[In the previous article, I examined the performance of some ruby and python interpreters on a Windows XP system. This time, I run the same script under Linux distribution: Ubuntu 9.10 2.6.31-20-generic. APT versions were installed via package manager, the RVM instead, through the Ruby Version Manager which build from sources. Version Builder/System/VM Seconds JRuby [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://mastrodonato.info/index.php/2010/01/ruby-vs-ruby-vs-python-vs-python/">previous article</a>, I examined the performance of some ruby and python interpreters on a Windows XP system. This time, I run the same script under Linux distribution: Ubuntu 9.10 2.6.31-20-generic. APT versions were installed via package manager, the RVM instead, through the <a href="http://rvm.beginrescueend.com/">Ruby Version Manager</a> which build from sources.</p>
<p><center></p>
<table>
<th>Version</th>
<th>Builder/System/VM</th>
<th>Seconds</th>
<tbody>
<tr>
<td>JRuby 1.4.0 APT</td>
<td> Java HotSpot(TM) 64-Bit Server VM 1.6.0_16) [amd64-java]</td>
<td><strong>6,1</strong></td>
</tr>
<tr>
<td>JRuby 1.4.0 RVM</td>
<td> Java HotSpot(TM) 64-Bit Server VM 1.6.0_16) [amd64-java]</td>
<td><strong>6,2</strong></td>
</tr>
<tr>
<td>Ruby 1.9.2 preview1 RVM</td>
<td>x86_64-linux</td>
<td><strong>6,5</strong></td>
</tr>
<tr>
<td>JRuby 1.4.0 </td>
<td>Windows Client VM 1.6.0_15 [x86-java]</td>
<td><strong>6,9</strong></td>
</tr>
<tr>
<td>Python 2.6.4</td>
<td>Windows</td>
<td>7,5</td>
</tr>
<tr>
<td>Ruby 1.9.1 p129</td>
<td>i386-mingw32</td>
<td>8,1</td>
</tr>
<tr>
<td>Python 2.6.4 APT</td>
<td>x86_64-linux</td>
<td>8,7</td>
</tr>
<tr>
<td>Ruby 1.9.1 p378 RVM</td>
<td>x86_64-linux</td>
<td>8,7</td>
</tr>
<tr>
<td>Ruby 1.9.1 p243 RVM</td>
<td>x86_64-linux</td>
<td>8,8</td>
</tr>
<tr>
<td>Python 3.1.1</td>
<td>Windows</td>
<td>9,0</td>
</tr>
<tr>
<td>Ruby 1.9.1 p243 APT</td>
<td>x86_64-linux</td>
<td>9,3</td>
</tr>
<tr>
<td>Ruby 1.9.1 p243</td>
<td>i386-mingw32</td>
<td>9,6</td>
</tr>
<tr>
<td>Ruby 1.8.7 p249 RVM</td>
<td>x86_64-linux</td>
<td>12,2</td>
</tr>
<tr>
<td>IronRuby 0.9.3.0</td>
<td>Windows .NET 2.0.0.0</td>
<td>18,9</td>
</tr>
<tr>
<td>Ruby 1.9.1 p376</td>
<td>i386-mswin32</td>
<td>20,8</td>
</tr>
<tr>
<td>Ruby 1.8.7 p174 APT</td>
<td>x86_64-linux</td>
<td>23,0</td>
</tr>
<tr>
<td>Ruby 1.8.6 p368</td>
<td>i386-mingw32</td>
<td>23,3</td>
</tr>
<tr>
<td>Ruby 1.8.7 p249</td>
<td>i386-mingw32</td>
<td>23,9</td>
</tr>
<tr>
<td>IronPython 2.6</td>
<td>Windows .NET 2.0.0.0</td>
<td>256,5</td>
</tr>
<tr>
<td>Jython 2.5.1</td>
<td>Windows Client VM 1.6.0_15 [x86-java]</td>
<td><em>Timeout</em></td>
</tr>
</tbody>
</table>
<p></center></p>
<div id="attachment_433" class="wp-caption aligncenter" style="width: 310px"><a href="http://mastrodonato.info/wp-content/uploads/2010/03/WinVsLinux.png"><img src="http://mastrodonato.info/wp-content/uploads/2010/03/WinVsLinux-300x173.png" alt="Win vs Linux" title="WinVsLinux" width="300" height="173" class="size-medium wp-image-433" /></a><p class="wp-caption-text">Win vs Linux</p></div>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2010/03/ruby-vs-python-vs-windows-vs-linux-2/feed/?lang=en</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ruby vs Ruby vs Python vs Python</title>
		<link>http://mastrodonato.info/index.php/2010/01/ruby-vs-ruby-vs-python-vs-python-2/?lang=en</link>
		<comments>http://mastrodonato.info/index.php/2010/01/ruby-vs-ruby-vs-python-vs-python-2/?lang=en#comments</comments>
		<pubDate>Sun, 24 Jan 2010 22:31:12 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[IronRuby .NET @en]]></category>
		<category><![CDATA[JRuby @en]]></category>
		<category><![CDATA[Ruby @en]]></category>
		<category><![CDATA[Benchmark]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=403</guid>
		<description><![CDATA[Another benchmark to compare some recent versions of ruby with the latest releases of Python. Two simple scripts to compare the syntax and performance of these modern languages. The system on which I performed the test is a Dell Inspiron 9400 with Centrino Duo, Intel T7200 4Mb Cache, 2GHz, 2GB RAM 667Mhz. Windows XP Pro [...]]]></description>
			<content:encoded><![CDATA[<p>Another benchmark to compare some recent versions of ruby with the latest releases of Python. Two simple scripts to compare the syntax and performance of these modern languages.<br />
The system on which I performed the test is a Dell Inspiron 9400 with Centrino Duo, Intel T7200 4Mb Cache, 2GHz, 2GB RAM 667Mhz. Windows XP Pro SP3.</p>
<p>This is the result of the first test, used to warming up for the VM:</p>
<p><center></p>
<table>
<th>Version</th>
<th>Builder</th>
<th>Seconds</th>
<tbody>
<tr>
<td>Python 2.6.4</td>
<td></td>
<td><strong>7,5</strong></td>
</tr>
<tr>
<td>Ruby 1.9.1 p129</td>
<td>i386-mingw32</td>
<td>8,2</td>
</tr>
<tr>
<td>JRuby 1.4.0 </td>
<td>Client VM 1.6.0_15 [x86-java]</td>
<td>9,0</td>
</tr>
<tr>
<td>Python 3.1.1</td>
<td></td>
<td>9,1</td>
</tr>
<tr>
<td>Ruby 1.9.1 p243</td>
<td>i386-mingw32</td>
<td>9,6</td>
</tr>
<tr>
<td>IronRuby 0.9.3.0</td>
<td>.NET 2.0.0.0</td>
<td>20,3</td>
</tr>
<tr>
<td>Ruby 1.9.1 p376</td>
<td>i386-mswin32</td>
<td>20,9</td>
</tr>
<tr>
<td>Ruby 1.8.6 p368</td>
<td>i386-mingw32</td>
<td>22,9</td>
</tr>
<tr>
<td>IronPython 2.6</td>
<td>.NET 2.0.0.0</td>
<td>225,4</td>
</tr>
<tr>
<td>Jython 2.5.1</td>
<td>Client VM 1.6.0_15 [x86-java]</td>
<td><em>Timeout</em></td>
</tr>
</tbody>
</table>
<p></center></p>
<div id="attachment_397" class="wp-caption aligncenter" style="width: 310px"><a href="http://mastrodonato.info/wp-content/uploads/2010/01/BenchMM1s.PNG"><img src="http://mastrodonato.info/wp-content/uploads/2010/01/BenchMM1s-300x121.PNG" alt="Benchmark without warm up" title="Bench MM1" width="300" height="121" class="size-medium wp-image-397" /></a><p class="wp-caption-text">Benchmark without warm up</p></div>
<p>The result below instead, refers to the average of three readings after the warm up.  JRuby&#8217;s performance improves by 23%:</p>
<p><center></p>
<table>
<th>Version</th>
<th>Builder</th>
<th>Seconds</th>
<tbody>
<tr>
<td>JRuby 1.4.0 </td>
<td>Client VM 1.6.0_15 [x86-java]</td>
<td><strong>6,9</strong></td>
</tr>
<tr>
<td>Python 2.6.4</td>
<td></td>
<td>7,5</td>
</tr>
<tr>
<td>Ruby 1.9.1 p129</td>
<td>i386-mingw32</td>
<td>8,2</td>
</tr>
<tr>
<td>Python 3.1.1</td>
<td></td>
<td>9,0</td>
</tr>
<tr>
<td>Ruby 1.9.1 p243</td>
<td>i386-mingw32</td>
<td>10,0</td>
</tr>
<tr>
<td>IronRuby 0.9.3.0</td>
<td>.NET 2.0.0.0</td>
<td>18,9</td>
</tr>
<tr>
<td>Ruby 1.9.1 p376</td>
<td>i386-mswin32</td>
<td>20,6</td>
</tr>
<tr>
<td>Ruby 1.8.6 p368</td>
<td>i386-mingw32</td>
<td>23,2</td>
</tr>
<tr>
<td>IronPython 2.6</td>
<td>.NET 2.0.0.0</td>
<td>256,5</td>
</tr>
<tr>
<td>Jython 2.5.1</td>
<td>Client VM 1.6.0_15 [x86-java]</td>
<td><em>Timeout</em></td>
</tr>
</tbody>
</table>
<p></center></p>
<div id="attachment_408" class="wp-caption aligncenter" style="width: 310px"><a href="http://mastrodonato.info/wp-content/uploads/2010/01/BenchMM1_warmup.PNG"><img src="http://mastrodonato.info/wp-content/uploads/2010/01/BenchMM1_warmup-300x121.PNG" alt="Benchmark after warm up" title="BenchMM1_warmup" width="300" height="121" class="size-medium wp-image-408" /></a><p class="wp-caption-text">Benchmark after warm up</p></div>
<p>And here is the scripts. I tried to optimize every versions and to do that, I had to create two variations for each language.</p>
<p><strong>Ruby 1.8.6:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> strings_test<span style="color:#006600; font-weight:bold;">&#40;</span>ntest<span style="color:#006600; font-weight:bold;">&#41;</span>
  r1 = r2 = r3 = <span style="color:#006666;">0</span>
  xstr = <span style="color:#996600;">&quot;&quot;</span>
  ntest.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span> 
    <span style="color:#008000; font-style:italic;">#Create a string, add 'abcde1234_' until getting a str size 1000</span>
    xstr = <span style="color:#996600;">'abcde1234_'</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">10000</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#Make letters upcase </span>
    xstr.<span style="color:#9900CC;">upcase</span>!
&nbsp;
    <span style="color:#008000; font-style:italic;">#Change '1234_' with '67890 ' (space at last position)</span>
    <span style="color:#008000; font-style:italic;">#Now the repeated string should be 'ABCDE67890 '</span>
    xstr.<span style="color:#CC0066; font-weight:bold;">gsub!</span> <span style="color:#996600;">'1234_'</span>, <span style="color:#996600;">'67890 '</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#Cast numbers from 29 upto size/2 to string and add it to xstr variable, ciclying for every number (not add all numbers one time)</span>
    29.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>xstr.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> xstr <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> n.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#Check 1: Count 'A' char </span>
    <span style="color:#008000; font-style:italic;">#Check 2: Count '9' char </span>
    0.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>xstr.<span style="color:#9900CC;">size</span><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>n<span style="color:#006600; font-weight:bold;">|</span> 
      <span style="color:#9966CC; font-weight:bold;">if</span> xstr<span style="color:#006600; font-weight:bold;">&#91;</span>n<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">chr</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#996600;">'A'</span>
        r1<span style="color:#006600; font-weight:bold;">+</span>=<span style="color:#006666;">1</span> 
      <span style="color:#9966CC; font-weight:bold;">elsif</span> xstr<span style="color:#006600; font-weight:bold;">&#91;</span>n<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">chr</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#996600;">'9'</span>
        r2<span style="color:#006600; font-weight:bold;">+</span>=<span style="color:#006666;">1</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#Create an array from xstr using space to split</span>
    r3 <span style="color:#006600; font-weight:bold;">+</span>= xstr.<span style="color:#CC0066; font-weight:bold;">split</span>.<span style="color:#9900CC;">size</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#0000FF; font-weight:bold;">return</span> r1, r2, r3, xstr
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> arrays_test<span style="color:#006600; font-weight:bold;">&#40;</span>ntest, xstr<span style="color:#006600; font-weight:bold;">&#41;</span>
  r1 = r2 = r3 = r4 = r5 = <span style="color:#006666;">0</span>
  ntest.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span> 
    <span style="color:#008000; font-style:italic;">#Clear ar then add 5000 times this element: &quot;I&quot;, &quot;am&quot;, &quot;great&quot;, null, &quot;or&quot;, &quot;number&quot;, 1</span>
    ar =  <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    5000.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'I'</span>, <span style="color:#996600;">'am'</span>, <span style="color:#996600;">'great'</span>, <span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#996600;">'or'</span>, <span style="color:#996600;">'number'</span>, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span> ar <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> a<span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#...then reverse elements to obtain this order: 1, &quot;number&quot;, &quot;or&quot;, null, &quot;great&quot;, &quot;am&quot;, &quot;I&quot;</span>
    ar.<span style="color:#9900CC;">reverse</span>!
&nbsp;
    <span style="color:#008000; font-style:italic;">#...then, count the element with value &quot;great&quot; using two separate cicle</span>
    <span style="color:#008000; font-style:italic;">#the first starting from 31 until 2955 (bounty inclused)</span>
    31.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2955</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>n<span style="color:#006600; font-weight:bold;">|</span>
      r1 <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">if</span> ar<span style="color:#006600; font-weight:bold;">&#91;</span>n<span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">'great'</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#008000; font-style:italic;">#the second looping all the array elements</span>
    ar.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> r2<span style="color:#006600; font-weight:bold;">+</span>=<span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">if</span> n == <span style="color:#996600;">'great'</span><span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#Loop inside and build a temporary string with index and value, without put it into a variable and only for elements &lt;&gt; null</span>
    ar.<span style="color:#9900CC;">each_index</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span> ar<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> ? <span style="color:#996600;">&quot;#{i} #{ar[i]}&quot;</span> : r3<span style="color:#006600; font-weight:bold;">+</span>=<span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#delete null value elements and take its size</span>
    ar.<span style="color:#9900CC;">compact</span>!
    r4 <span style="color:#006600; font-weight:bold;">+</span>= ar.<span style="color:#9900CC;">size</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#then join elements with space and take its size</span>
    r5 <span style="color:#006600; font-weight:bold;">+</span>= ar.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">' '</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">size</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#0000FF; font-weight:bold;">return</span> r1, r2, r3, r4, r5
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> nums_test<span style="color:#006600; font-weight:bold;">&#40;</span>ntest<span style="color:#006600; font-weight:bold;">&#41;</span>
  r1 = r2 = <span style="color:#006666;">0</span>
&nbsp;
  ntest.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span> 
    <span style="color:#008000; font-style:italic;">#Find all prime numbers from 8 to 95 step by 3 and sum all primes got, to check the result</span>
    <span style="color:#008000; font-style:italic;">#51.upto(307) do |n| </span>
    <span style="color:#006600; font-weight:bold;">&#40;</span>8..95<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">step</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">3</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>n<span style="color:#006600; font-weight:bold;">|</span>
      primes<span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span> r1 <span style="color:#006600; font-weight:bold;">+</span>= a<span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#Calculate factorial numbers start from 2 to 42</span>
    r2 = <span style="color:#006666;">0</span>
    <span style="color:#9966CC; font-weight:bold;">for</span> n <span style="color:#9966CC; font-weight:bold;">in</span> 2..42
      r2 <span style="color:#006600; font-weight:bold;">+</span>= fac<span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#0000FF; font-weight:bold;">return</span> r1, r2
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#Primes must return an array of prime numbers</span>
<span style="color:#9966CC; font-weight:bold;">def</span> primes<span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span>
  ar = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">for</span> x <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006600; font-weight:bold;">&#40;</span>2..<span style="color:#9900CC;">n</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    prime = <span style="color:#0000FF; font-weight:bold;">true</span>
    <span style="color:#9966CC; font-weight:bold;">for</span> y <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006600; font-weight:bold;">&#40;</span>2..<span style="color:#9900CC;">x</span><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;">if</span> x<span style="color:#006600; font-weight:bold;">%</span>y == <span style="color:#006666;">0</span>
        prime = <span style="color:#0000FF; font-weight:bold;">false</span>
        <span style="color:#9966CC; font-weight:bold;">break</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    ar <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> x <span style="color:#9966CC; font-weight:bold;">if</span> prime
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> ar
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> fac<span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>1..<span style="color:#9900CC;">n</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>total, current<span style="color:#006600; font-weight:bold;">|</span> total <span style="color:#006600; font-weight:bold;">*</span> current<span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># ---  START  ---</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>Warming up...&quot;</span>
t1=t2=t3=<span style="color:#006666;">0</span>
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2, r3, xstr = strings_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Strings test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p1=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check1: #{r1}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check2: #{r2}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check3: #{r3}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check4: #{xstr.size}&quot;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2, r3, r4, r5 = arrays_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">50</span>, xstr<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Arrays test  - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p2=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check1: #{r1}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check2: #{r2}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check3: #{r3}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check4: #{r4}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check5: #{r5}&quot;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2 = nums_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">500</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Numeric test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p3=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check1: #{r1}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check2: #{r2}&quot;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Ruby Partial elapsed time %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p1<span style="color:#006600; font-weight:bold;">+</span>p2<span style="color:#006600; font-weight:bold;">+</span>p3<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>1. Starting Ruby tests...&quot;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2, r3, xstr = strings_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Strings test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p1=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2, r3, r4, r5 = arrays_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">50</span>, xstr<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Arrays test  - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p2=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2 = nums_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">500</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Numeric test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p3=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Ruby Partial elapsed time %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p1<span style="color:#006600; font-weight:bold;">+</span>p2<span style="color:#006600; font-weight:bold;">+</span>p3<span style="color:#006600; font-weight:bold;">&#41;</span>
t1<span style="color:#006600; font-weight:bold;">+</span>=p1;t2<span style="color:#006600; font-weight:bold;">+</span>=p2;t3<span style="color:#006600; font-weight:bold;">+</span>=p3
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>2. Starting Ruby tests...&quot;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2, r3, xstr = strings_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Strings test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p1=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2, r3, r4, r5 = arrays_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">50</span>, xstr<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Arrays test  - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p2=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2 = nums_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">500</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Numeric test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p3=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Ruby Partial elapsed time %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p1<span style="color:#006600; font-weight:bold;">+</span>p2<span style="color:#006600; font-weight:bold;">+</span>p3<span style="color:#006600; font-weight:bold;">&#41;</span>
t1<span style="color:#006600; font-weight:bold;">+</span>=p1;t2<span style="color:#006600; font-weight:bold;">+</span>=p2;t3<span style="color:#006600; font-weight:bold;">+</span>=p3
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>3. Starting Ruby tests...&quot;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2, r3, xstr = strings_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Strings test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p1=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2, r3, r4, r5 = arrays_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">50</span>, xstr<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Arrays test  - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p2=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2 = nums_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">500</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Numeric test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p3=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Ruby Partial elapsed time %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p1<span style="color:#006600; font-weight:bold;">+</span>p2<span style="color:#006600; font-weight:bold;">+</span>p3<span style="color:#006600; font-weight:bold;">&#41;</span>
t1<span style="color:#006600; font-weight:bold;">+</span>=p1;t2<span style="color:#006600; font-weight:bold;">+</span>=p2;t3<span style="color:#006600; font-weight:bold;">+</span>=p3
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;-------------------------------------&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Average Strings test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>t1<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Average Arrays test  - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>t2<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Average Numeric test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>t3<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>Ruby Average elapsed time %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>t1<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">+</span>t2<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">+</span>t3<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p><strong>Ruby 1.9.1:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> strings_test<span style="color:#006600; font-weight:bold;">&#40;</span>ntest<span style="color:#006600; font-weight:bold;">&#41;</span>
  r1 = r2 = r3 = <span style="color:#006666;">0</span>
  xstr = <span style="color:#996600;">&quot;&quot;</span>
  ntest.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span> 
    <span style="color:#008000; font-style:italic;">#Create a string, add 'abcde1234_' until getting a str size 1000</span>
    xstr = <span style="color:#996600;">'abcde1234_'</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">10000</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#Make letters upcase </span>
    xstr.<span style="color:#9900CC;">upcase</span>!
&nbsp;
    <span style="color:#008000; font-style:italic;">#Change '1234_' with '67890 ' (space at last position)</span>
    <span style="color:#008000; font-style:italic;">#Now the repeated string should be 'ABCDE67890 '</span>
    xstr.<span style="color:#CC0066; font-weight:bold;">gsub!</span> <span style="color:#996600;">'1234_'</span>, <span style="color:#996600;">'67890 '</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#Cast numbers to string, from 29 up to size/2. Add it to xstr variable as well, ciclying for every number (not adding all numbers once)</span>
    29.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>xstr.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> xstr <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> n.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#Check 1: Count 'A' char </span>
    <span style="color:#008000; font-style:italic;">#Check 2: Count '9' char </span>
    0.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>xstr.<span style="color:#9900CC;">size</span><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>n<span style="color:#006600; font-weight:bold;">|</span> 
      <span style="color:#9966CC; font-weight:bold;">if</span> xstr<span style="color:#006600; font-weight:bold;">&#91;</span>n<span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">'A'</span>
        r1<span style="color:#006600; font-weight:bold;">+</span>=<span style="color:#006666;">1</span> 
      <span style="color:#9966CC; font-weight:bold;">elsif</span> xstr<span style="color:#006600; font-weight:bold;">&#91;</span>n<span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">'9'</span>
        r2<span style="color:#006600; font-weight:bold;">+</span>=<span style="color:#006666;">1</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#Create an array from xstr using space to split, its size is the third check</span>
    r3 <span style="color:#006600; font-weight:bold;">+</span>= xstr.<span style="color:#CC0066; font-weight:bold;">split</span>.<span style="color:#9900CC;">size</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#0000FF; font-weight:bold;">return</span> r1, r2, r3, xstr
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> arrays_test<span style="color:#006600; font-weight:bold;">&#40;</span>ntest, xstr<span style="color:#006600; font-weight:bold;">&#41;</span>
  r1 = r2 = r3 = r4 = r5 = <span style="color:#006666;">0</span>
  ntest.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span> 
    <span style="color:#008000; font-style:italic;">#Clear ar then add 5000 times this element: &quot;I&quot;, &quot;am&quot;, &quot;great&quot;, null, &quot;or&quot;, &quot;number&quot;, 1</span>
    ar =  <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    5000.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'I'</span>, <span style="color:#996600;">'am'</span>, <span style="color:#996600;">'great'</span>, <span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#996600;">'or'</span>, <span style="color:#996600;">'number'</span>, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span> ar <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> a<span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#...then reverse elements to obtain this order: 1, &quot;number&quot;, &quot;or&quot;, null, &quot;great&quot;, &quot;am&quot;, &quot;I&quot;</span>
    ar.<span style="color:#9900CC;">reverse</span>!
&nbsp;
    <span style="color:#008000; font-style:italic;">#...then, count the element with value &quot;great&quot; using two separate cicle</span>
    <span style="color:#008000; font-style:italic;">#the first starting from 31 until 2955 (bounty inclused)</span>
    31.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2955</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>n<span style="color:#006600; font-weight:bold;">|</span>
      r1 <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">if</span> ar<span style="color:#006600; font-weight:bold;">&#91;</span>n<span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">'great'</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#008000; font-style:italic;">#the second looping all the array elements</span>
    ar.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> r2<span style="color:#006600; font-weight:bold;">+</span>=<span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">if</span> n == <span style="color:#996600;">'great'</span><span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#Loop inside and build a temporary string with index and value, without put it into a variable and only for elements &lt;&gt; null</span>
    ar.<span style="color:#9900CC;">each_index</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span> ar<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> ? <span style="color:#996600;">&quot;#{i} #{ar[i]}&quot;</span> : r3<span style="color:#006600; font-weight:bold;">+</span>=<span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#delete null value elements and take its size</span>
    ar.<span style="color:#9900CC;">compact</span>!
    r4 <span style="color:#006600; font-weight:bold;">+</span>= ar.<span style="color:#9900CC;">size</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#then join elements with space and take its size</span>
    r5 <span style="color:#006600; font-weight:bold;">+</span>= ar.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">' '</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">size</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#0000FF; font-weight:bold;">return</span> r1, r2, r3, r4, r5
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> nums_test<span style="color:#006600; font-weight:bold;">&#40;</span>ntest<span style="color:#006600; font-weight:bold;">&#41;</span>
  r1 = r2 = <span style="color:#006666;">0</span>
&nbsp;
  ntest.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span> 
    <span style="color:#008000; font-style:italic;">#Find all prime numbers from 8 to 95 step by 3 (bounds included) and sum all primes got, to check the result</span>
    <span style="color:#008000; font-style:italic;">#51.upto(307) do |n| </span>
    <span style="color:#006600; font-weight:bold;">&#40;</span>8..95<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">step</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">3</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>n<span style="color:#006600; font-weight:bold;">|</span>
      primes<span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span> r1 <span style="color:#006600; font-weight:bold;">+</span>= a<span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#Calculate factorial numbers start from 2 to 42</span>
    r2 = <span style="color:#006666;">0</span>
    <span style="color:#9966CC; font-weight:bold;">for</span> n <span style="color:#9966CC; font-weight:bold;">in</span> 2..42
      r2 <span style="color:#006600; font-weight:bold;">+</span>= fac<span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#0000FF; font-weight:bold;">return</span> r1, r2
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#Primes must return an array of prime numbers</span>
<span style="color:#9966CC; font-weight:bold;">def</span> primes<span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span>
  ar = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">for</span> x <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006600; font-weight:bold;">&#40;</span>2..<span style="color:#9900CC;">n</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    prime = <span style="color:#0000FF; font-weight:bold;">true</span>
    <span style="color:#9966CC; font-weight:bold;">for</span> y <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006600; font-weight:bold;">&#40;</span>2..<span style="color:#9900CC;">x</span><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;">if</span> x<span style="color:#006600; font-weight:bold;">%</span>y == <span style="color:#006666;">0</span>
        prime = <span style="color:#0000FF; font-weight:bold;">false</span>
        <span style="color:#9966CC; font-weight:bold;">break</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    ar <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> x <span style="color:#9966CC; font-weight:bold;">if</span> prime
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> ar
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> fac<span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>1..<span style="color:#9900CC;">n</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>total, current<span style="color:#006600; font-weight:bold;">|</span> total <span style="color:#006600; font-weight:bold;">*</span> current<span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># ---  START  ---</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>Warming up...&quot;</span>
t1=t2=t3=<span style="color:#006666;">0</span>
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2, r3, xstr = strings_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Strings test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p1=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check1: #{r1}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check2: #{r2}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check3: #{r3}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check4: #{xstr.size}&quot;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2, r3, r4, r5 = arrays_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">50</span>, xstr<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Arrays test  - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p2=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check1: #{r1}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check2: #{r2}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check3: #{r3}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check4: #{r4}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check5: #{r5}&quot;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2 = nums_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">500</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Numeric test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p3=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check1: #{r1}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Check2: #{r2}&quot;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Ruby Partial elapsed time %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p1<span style="color:#006600; font-weight:bold;">+</span>p2<span style="color:#006600; font-weight:bold;">+</span>p3<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>1. Starting Ruby tests...&quot;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2, r3, xstr = strings_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Strings test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p1=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2, r3, r4, r5 = arrays_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">50</span>, xstr<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Arrays test  - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p2=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2 = nums_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">500</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Numeric test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p3=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Ruby Partial elapsed time %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p1<span style="color:#006600; font-weight:bold;">+</span>p2<span style="color:#006600; font-weight:bold;">+</span>p3<span style="color:#006600; font-weight:bold;">&#41;</span>
t1<span style="color:#006600; font-weight:bold;">+</span>=p1;t2<span style="color:#006600; font-weight:bold;">+</span>=p2;t3<span style="color:#006600; font-weight:bold;">+</span>=p3
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>2. Starting Ruby tests...&quot;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2, r3, xstr = strings_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Strings test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p1=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2, r3, r4, r5 = arrays_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">50</span>, xstr<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Arrays test  - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p2=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2 = nums_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">500</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Numeric test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p3=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Ruby Partial elapsed time %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p1<span style="color:#006600; font-weight:bold;">+</span>p2<span style="color:#006600; font-weight:bold;">+</span>p3<span style="color:#006600; font-weight:bold;">&#41;</span>
t1<span style="color:#006600; font-weight:bold;">+</span>=p1;t2<span style="color:#006600; font-weight:bold;">+</span>=p2;t3<span style="color:#006600; font-weight:bold;">+</span>=p3
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>3. Starting Ruby tests...&quot;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2, r3, xstr = strings_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Strings test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p1=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2, r3, r4, r5 = arrays_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">50</span>, xstr<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Arrays test  - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p2=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
r1, r2 = nums_test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">500</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Numeric test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p3=<span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Ruby Partial elapsed time %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>p1<span style="color:#006600; font-weight:bold;">+</span>p2<span style="color:#006600; font-weight:bold;">+</span>p3<span style="color:#006600; font-weight:bold;">&#41;</span>
t1<span style="color:#006600; font-weight:bold;">+</span>=p1;t2<span style="color:#006600; font-weight:bold;">+</span>=p2;t3<span style="color:#006600; font-weight:bold;">+</span>=p3
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;-------------------------------------&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Average Strings test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>t1<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Average Arrays test  - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>t2<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Average Numeric test - Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>t3<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>Ruby Average elapsed time %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span>t1<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">+</span>t2<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">+</span>t3<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p><strong>Python 2.6:</strong></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> <span style="color: #dc143c;">time</span>
<span style="color: #808080; font-style: italic;">#import psyco</span>
<span style="color: #808080; font-style: italic;">#psyco.full()</span>
<span style="color: #808080; font-style: italic;">#psyco.full(memory=100)</span>
<span style="color: #808080; font-style: italic;">#psyco.profile(0.05, memory=100)</span>
<span style="color: #808080; font-style: italic;">#psyco.profile(0.2)</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> strings_test<span style="color: black;">&#40;</span>ntest<span style="color: black;">&#41;</span>:
  r1 = r2 = r3 = <span style="color: #ff4500;">0</span>
  xstr = <span style="color: #483d8b;">&quot;&quot;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>ntest<span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;">#Create a string, add 'abcde1234_' until getting a xstr size 1000</span>
    xstr = <span style="color: #483d8b;">'abcde1234_'</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">10000</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Make letters upcase </span>
    xstr = xstr.<span style="color: black;">upper</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Change '1234_' with '67890 ' (space at last position)</span>
    <span style="color: #808080; font-style: italic;">#Now the repeated string should be 'ABCDE67890 '</span>
    xstr = xstr.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'1234_'</span>, <span style="color: #483d8b;">'67890 '</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Cast numbers to string, from 29 up to size/2. Add it to xstr variable as well, ciclying for every number (not adding all numbers once)</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> y <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;">29</span>,<span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>xstr<span style="color: black;">&#41;</span>/<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
        xstr += <span style="color: #483d8b;">&quot;%s&quot;</span> <span style="color: #66cc66;">%</span>y
&nbsp;
    <span style="color: #808080; font-style: italic;">#Result 1: Count 'A' char </span>
    <span style="color: #808080; font-style: italic;">#Result 2: Count '9' char </span>
    <span style="color: #ff7700;font-weight:bold;">for</span> y <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;">0</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>xstr<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> xstr<span style="color: black;">&#91;</span>y<span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'A'</span>:
        r1+=<span style="color: #ff4500;">1</span> 
      <span style="color: #ff7700;font-weight:bold;">elif</span> xstr<span style="color: black;">&#91;</span>y<span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'9'</span>:
        r2+=<span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Create an array from xstr using space to split</span>
    r3 += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>xstr.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">return</span> r1, r2, r3, xstr
&nbsp;
<span style="color: #808080; font-style: italic;">#Slower than other version</span>
<span style="color: #ff7700;font-weight:bold;">def</span> multiremove<span style="color: black;">&#40;</span>ar, what<span style="color: black;">&#41;</span>:
  i = <span style="color: #ff4500;">0</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> el <span style="color: #ff7700;font-weight:bold;">in</span> ar:
    <span style="color: #ff7700;font-weight:bold;">if</span> el == what:
      <span style="color: #ff7700;font-weight:bold;">del</span> ar<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>
    i+=<span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Ugly but a bit faster</span>
<span style="color: #ff7700;font-weight:bold;">def</span> multiremove2<span style="color: black;">&#40;</span>ar, what<span style="color: black;">&#41;</span>:
  todel = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span> 
  <span style="color: #ff7700;font-weight:bold;">for</span> y <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;">0</span>,<span style="color: #008000;">len</span><span style="color: black;">&#40;</span>ar<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> ar<span style="color: black;">&#91;</span>y<span style="color: black;">&#93;</span> == what:
       todel.<span style="color: black;">append</span><span style="color: black;">&#40;</span>y<span style="color: black;">&#41;</span>
  todel.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> y <span style="color: #ff7700;font-weight:bold;">in</span> todel:
     ar.<span style="color: black;">pop</span><span style="color: black;">&#40;</span>y<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> arrays_test<span style="color: black;">&#40;</span>ntest, xstr<span style="color: black;">&#41;</span>:
  r1 = r2 = r3 = r4 = r5 = <span style="color: #ff4500;">0</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>ntest<span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;">#Clear ar then add 5000 times this element: &quot;I&quot;, &quot;am&quot;, &quot;great&quot;, null, &quot;or&quot;, &quot;number&quot;, 1</span>
    ar = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> y <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;">0</span>, <span style="color: #ff4500;">5000</span><span style="color: black;">&#41;</span>:
      ar.<span style="color: black;">extend</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;I&quot;</span>, <span style="color: #483d8b;">&quot;am&quot;</span>, <span style="color: #483d8b;">&quot;great&quot;</span>, <span style="color: #008000;">None</span>, <span style="color: #483d8b;">&quot;or&quot;</span>, <span style="color: #483d8b;">&quot;number&quot;</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#...then reverse elements to obtain this order: 1, &quot;number&quot;, &quot;or&quot;, null, &quot;great&quot;, &quot;am&quot;, &quot;I&quot;</span>
    ar.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#...then, count the element with value &quot;great&quot; using two separate cicle</span>
    <span style="color: #808080; font-style: italic;">#the first starting from 31 until 2955 (bounty included)</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> y <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;">31</span>,<span style="color: #ff4500;">2955</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> ar<span style="color: black;">&#91;</span>y<span style="color: black;">&#93;</span> == <span style="color: #483d8b;">&quot;great&quot;</span>: r1 +=<span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#the second looping all the array elements</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> y <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;">0</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>ar<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> ar<span style="color: black;">&#91;</span>y<span style="color: black;">&#93;</span> == <span style="color: #483d8b;">&quot;great&quot;</span>: r2+=<span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Loop inside and build a temporary string with index and value, without put it into a variable and only for elements &lt;&gt; null</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> y <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;">0</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>ar<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> ar<span style="color: black;">&#91;</span>y<span style="color: black;">&#93;</span>:
        <span style="color: #483d8b;">&quot;%s %s&quot;</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#40;</span>y, ar<span style="color: black;">&#91;</span>y<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">else</span>:
        r3+=<span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#delete null value elements and take its size</span>
    multiremove2<span style="color: black;">&#40;</span>ar, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
    r4 += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>ar<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#then join elements with space and take its size</span>
    r5 += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; &quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> ar<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">return</span> r1, r2, r3, r4, r5
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> nums_test<span style="color: black;">&#40;</span>ntest<span style="color: black;">&#41;</span>:
  r1 = r2 = <span style="color: #ff4500;">0</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>ntest<span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;">#Find all prime numbers from 8 to 95 step by 3 (bounds included) and sum all primes got, to check the result</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> n <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;">8</span>, <span style="color: #ff4500;">96</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">for</span> prime <span style="color: #ff7700;font-weight:bold;">in</span> primes<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
        r1 += prime
    fac = <span style="color: #ff7700;font-weight:bold;">lambda</span> n:<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>n<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">or</span> fac<span style="color: black;">&#40;</span>n-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span>n
    <span style="color: #808080; font-style: italic;">#Calculate factorial numbers start from 2 to 42 (bounds included)</span>
    r2 = <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> n <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;">43</span><span style="color: black;">&#41;</span>:
      r2 += fac<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">return</span> r1, r2
&nbsp;
<span style="color: #808080; font-style: italic;">#Primes must return an array of prime numbers</span>
<span style="color: #ff7700;font-weight:bold;">def</span> primes<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  ar = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> x <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>, n+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    prime = <span style="color: #008000;">True</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> y <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>, x<span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> x<span style="color: #66cc66;">%</span>y == <span style="color: #ff4500;">0</span>:
        prime = <span style="color: #008000;">False</span>
        <span style="color: #ff7700;font-weight:bold;">break</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> prime:
      ar.<span style="color: black;">append</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> ar
&nbsp;
<span style="color: #808080; font-style: italic;"># ---  START  ---</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Warming up...&quot;</span>
t1=t2=t3=<span style="color: #ff4500;">0</span>
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2, r3, xstr = strings_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
p1=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Strings test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p1<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Check1: %s&quot;</span> <span style="color: #66cc66;">%</span>r1
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Check2: %s&quot;</span> <span style="color: #66cc66;">%</span>r2
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Check3: %s&quot;</span> <span style="color: #66cc66;">%</span>r3
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Check4: %d&quot;</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>xstr<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2, r3, r4, r5 = arrays_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">50</span>, xstr<span style="color: black;">&#41;</span>
p2=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span>  <span style="color: #483d8b;">&quot;Arrays test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p2<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Check1: %s&quot;</span> <span style="color: #66cc66;">%</span>r1
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Check2: %s&quot;</span> <span style="color: #66cc66;">%</span>r2
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Check3: %s&quot;</span> <span style="color: #66cc66;">%</span>r3
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Check4: %s&quot;</span> <span style="color: #66cc66;">%</span>r4
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Check5: %s&quot;</span> <span style="color: #66cc66;">%</span>r5
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2 = nums_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">500</span><span style="color: black;">&#41;</span>
p3=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Numeric test  - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p3<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Check1: %s&quot;</span> <span style="color: #66cc66;">%</span>r1
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Check2: %s&quot;</span> <span style="color: #66cc66;">%</span>r2
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span>  <span style="color: #483d8b;">&quot;Python Partial elapsed time %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p1+p2+p3<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>1. Starting Python tests...&quot;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2, r3, xstr = strings_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
p1=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Strings test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p1<span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2, r3, r4, r5 = arrays_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">50</span>, xstr<span style="color: black;">&#41;</span>
p2=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span>  <span style="color: #483d8b;">&quot;Arrays test  - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p2<span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2 = nums_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">500</span><span style="color: black;">&#41;</span>
p3=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Numeric test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p3<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span>  <span style="color: #483d8b;">&quot;Python Partial elapsed time %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p1+p2+p3<span style="color: black;">&#41;</span>
t1+=p1<span style="color: #66cc66;">;</span>t2+=p2<span style="color: #66cc66;">;</span>t3+=p3
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>2. Starting Python tests...&quot;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2, r3, xstr = strings_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
p1=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Strings test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p1<span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2, r3, r4, r5 = arrays_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">50</span>, xstr<span style="color: black;">&#41;</span>
p2=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span>  <span style="color: #483d8b;">&quot;Arrays test  - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p2<span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2 = nums_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">500</span><span style="color: black;">&#41;</span>
p3=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Numeric test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p3<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span>  <span style="color: #483d8b;">&quot;Python Partial elapsed time %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p1+p2+p3<span style="color: black;">&#41;</span>
t1+=p1<span style="color: #66cc66;">;</span>t2+=p2<span style="color: #66cc66;">;</span>t3+=p3
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>3. Starting Python tests...&quot;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2, r3, xstr = strings_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
p1=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Strings test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p1<span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2, r3, r4, r5 = arrays_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">50</span>, xstr<span style="color: black;">&#41;</span>
p2=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span>  <span style="color: #483d8b;">&quot;Arrays test  - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p2<span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2 = nums_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">500</span><span style="color: black;">&#41;</span>
p3=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Numeric test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p3<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span>  <span style="color: #483d8b;">&quot;Python Partial elapsed time %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p1+p2+p3<span style="color: black;">&#41;</span>
t1+=p1<span style="color: #66cc66;">;</span>t2+=p2<span style="color: #66cc66;">;</span>t3+=p3
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;-------------------------------------&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Average Strings test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>t1/<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Average Arrays test  - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>t2/<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Average Numeric test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>t3/<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Python Average elapsed time %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>t1/<span style="color: #ff4500;">3</span>+t2/<span style="color: #ff4500;">3</span>+t3/<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span></pre></div></div>

<p><strong>Python 3.1:</strong></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> <span style="color: #dc143c;">time</span>
<span style="color: #808080; font-style: italic;">#import psyco</span>
<span style="color: #808080; font-style: italic;">#psyco.full()</span>
<span style="color: #808080; font-style: italic;">#psyco.full(memory=100)</span>
<span style="color: #808080; font-style: italic;">#psyco.profile(0.05, memory=100)</span>
<span style="color: #808080; font-style: italic;">#psyco.profile(0.2)</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> strings_test<span style="color: black;">&#40;</span>ntest<span style="color: black;">&#41;</span>:
  r1 = r2 = r3 = <span style="color: #ff4500;">0</span>
  xstr = <span style="color: #483d8b;">&quot;&quot;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>ntest<span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;">#Create a string, add 'abcde1234_' until getting a xstr size 1000</span>
    xstr = <span style="color: #483d8b;">'abcde1234_'</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">10000</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Make letters upcase </span>
    xstr = xstr.<span style="color: black;">upper</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Change '1234_' with '67890 ' (space at last position)</span>
    <span style="color: #808080; font-style: italic;">#Now the repeated string should be 'ABCDE67890 '</span>
    xstr = xstr.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'1234_'</span>, <span style="color: #483d8b;">'67890 '</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Cast numbers from 29 upto 1028 to string and add it to xstr variable, ciclying for every number (not add all numbers one time)</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> y <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">29</span>,<span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>xstr<span style="color: black;">&#41;</span>/<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
        xstr += <span style="color: #483d8b;">&quot;%s&quot;</span> <span style="color: #66cc66;">%</span>y
&nbsp;
    <span style="color: #808080; font-style: italic;">#Result 1: Count 'A' char </span>
    <span style="color: #808080; font-style: italic;">#Result 2: Count '9' char </span>
    <span style="color: #ff7700;font-weight:bold;">for</span> y <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>xstr<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> xstr<span style="color: black;">&#91;</span>y<span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'A'</span>:
        r1+=<span style="color: #ff4500;">1</span> 
      <span style="color: #ff7700;font-weight:bold;">elif</span> xstr<span style="color: black;">&#91;</span>y<span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'9'</span>:
        r2+=<span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Create an array from xstr using space to split</span>
    r3 += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>xstr.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">return</span> r1, r2, r3, xstr
&nbsp;
<span style="color: #808080; font-style: italic;">#Slower than other version</span>
<span style="color: #ff7700;font-weight:bold;">def</span> multiremove<span style="color: black;">&#40;</span>ar, what<span style="color: black;">&#41;</span>:
  i = <span style="color: #ff4500;">0</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> el <span style="color: #ff7700;font-weight:bold;">in</span> ar:
    <span style="color: #ff7700;font-weight:bold;">if</span> el == what:
      <span style="color: #ff7700;font-weight:bold;">del</span> ar<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>
    i+=<span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Ugly but a bit faster</span>
<span style="color: #ff7700;font-weight:bold;">def</span> multiremove2<span style="color: black;">&#40;</span>ar, what<span style="color: black;">&#41;</span>:
  todel = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span> 
  <span style="color: #ff7700;font-weight:bold;">for</span> y <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #008000;">len</span><span style="color: black;">&#40;</span>ar<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> ar<span style="color: black;">&#91;</span>y<span style="color: black;">&#93;</span> == what:
       todel.<span style="color: black;">append</span><span style="color: black;">&#40;</span>y<span style="color: black;">&#41;</span>
  todel.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> y <span style="color: #ff7700;font-weight:bold;">in</span> todel:
     ar.<span style="color: black;">pop</span><span style="color: black;">&#40;</span>y<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> arrays_test<span style="color: black;">&#40;</span>ntest, xstr<span style="color: black;">&#41;</span>:
  r1 = r2 = r3 = r4 = r5 = <span style="color: #ff4500;">0</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>ntest<span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;">#Clear ar then add 5000 times this element: &quot;I&quot;, &quot;am&quot;, &quot;great&quot;, null, &quot;or&quot;, &quot;number&quot;, 1</span>
    ar = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> y <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">5000</span><span style="color: black;">&#41;</span>:
      ar.<span style="color: black;">extend</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;I&quot;</span>, <span style="color: #483d8b;">&quot;am&quot;</span>, <span style="color: #483d8b;">&quot;great&quot;</span>, <span style="color: #008000;">None</span>, <span style="color: #483d8b;">&quot;or&quot;</span>, <span style="color: #483d8b;">&quot;number&quot;</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#...then reverse elements to obtain this order: 1, &quot;number&quot;, &quot;or&quot;, null, &quot;great&quot;, &quot;am&quot;, &quot;I&quot;</span>
    ar.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#...then, count the element with value &quot;great&quot; using two separate cicle</span>
    <span style="color: #808080; font-style: italic;">#the first starting from 31 until 2955 (bounty included)</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> y <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">31</span>,<span style="color: #ff4500;">2955</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> ar<span style="color: black;">&#91;</span>y<span style="color: black;">&#93;</span> == <span style="color: #483d8b;">&quot;great&quot;</span>: r1 +=<span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#the second looping all the array elements</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> y <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>ar<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> ar<span style="color: black;">&#91;</span>y<span style="color: black;">&#93;</span> == <span style="color: #483d8b;">&quot;great&quot;</span>: r2+=<span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Loop inside and build a temporary string with index and value, without put it into a variable and only for elements &lt;&gt; null</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> y <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>ar<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> ar<span style="color: black;">&#91;</span>y<span style="color: black;">&#93;</span>:
        <span style="color: #483d8b;">&quot;%s %s&quot;</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#40;</span>y, ar<span style="color: black;">&#91;</span>y<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">else</span>:
        r3+=<span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#delete null value elements and take its size</span>
    multiremove2<span style="color: black;">&#40;</span>ar, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
    r4 += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>ar<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#then join elements with space and take its size</span>
    r5 += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; &quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> ar<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">return</span> r1, r2, r3, r4, r5
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> nums_test<span style="color: black;">&#40;</span>ntest<span style="color: black;">&#41;</span>:
  r1 = r2 = <span style="color: #ff4500;">0</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>ntest<span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;">#Find all prime numbers from 8 to 95 step by 3 (bounds included) and sum all primes got, to check the result</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">8</span>, <span style="color: #ff4500;">96</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">for</span> prime <span style="color: #ff7700;font-weight:bold;">in</span> primes<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
        r1 += prime
    fac = <span style="color: #ff7700;font-weight:bold;">lambda</span> n:<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>n<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">or</span> fac<span style="color: black;">&#40;</span>n-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span>n
    <span style="color: #808080; font-style: italic;">#Calculate factorial numbers start from 2 to 42 (bounds included)</span>
    r2 = <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">43</span><span style="color: black;">&#41;</span>:
      r2 += fac<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">return</span> r1, r2
&nbsp;
<span style="color: #808080; font-style: italic;">#Primes must return an array of prime numbers</span>
<span style="color: #ff7700;font-weight:bold;">def</span> primes<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  ar = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, n+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    prime = <span style="color: #008000;">True</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> y <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, x<span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> x<span style="color: #66cc66;">%</span>y == <span style="color: #ff4500;">0</span>:
        prime = <span style="color: #008000;">False</span>
        <span style="color: #ff7700;font-weight:bold;">break</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> prime:
      ar.<span style="color: black;">append</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> ar
&nbsp;
<span style="color: #808080; font-style: italic;"># ---  START  ---</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Warming up...&quot;</span><span style="color: black;">&#41;</span>
t1=t2=t3=<span style="color: #ff4500;">0</span>
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2, r3, xstr = strings_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
p1=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Strings test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> p1<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Check1: %s&quot;</span> <span style="color: #66cc66;">%</span> r1<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Check2: %s&quot;</span> <span style="color: #66cc66;">%</span> r2<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Check3: %s&quot;</span> <span style="color: #66cc66;">%</span> r3<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Check4: %d&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>xstr<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2, r3, r4, r5 = arrays_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">50</span>, xstr<span style="color: black;">&#41;</span>
p2=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Arrays test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> p2<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Check1: %s&quot;</span> <span style="color: #66cc66;">%</span> r1<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Check2: %s&quot;</span> <span style="color: #66cc66;">%</span> r2<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Check3: %s&quot;</span> <span style="color: #66cc66;">%</span> r3<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Check4: %s&quot;</span> <span style="color: #66cc66;">%</span> r4<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Check5: %s&quot;</span> <span style="color: #66cc66;">%</span> r5<span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2 = nums_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">500</span><span style="color: black;">&#41;</span>
p3=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Numeric test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> p3<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Check1: %s&quot;</span> <span style="color: #66cc66;">%</span> r1<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Check2: %s&quot;</span> <span style="color: #66cc66;">%</span> r2<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;Python Partial elapsed time %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p1+p2+p3<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>1. Starting Python tests...&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2, r3, xstr = strings_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
p1=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Strings test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> p1<span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2, r3, r4, r5 = arrays_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">50</span>, xstr<span style="color: black;">&#41;</span>
p2=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;Arrays test  - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> p2<span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2 = nums_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">500</span><span style="color: black;">&#41;</span>
p3=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Numeric test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> p3<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;Python Partial elapsed time %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p1+p2+p3<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
t1+=p1<span style="color: #66cc66;">;</span>t2+=p2<span style="color: #66cc66;">;</span>t3+=p3
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>2. Starting Python tests...&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2, r3, xstr = strings_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
p1=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Strings test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> p1<span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2, r3, r4, r5 = arrays_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">50</span>, xstr<span style="color: black;">&#41;</span>
p2=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;Arrays test  - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> p2<span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2 = nums_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">500</span><span style="color: black;">&#41;</span>
p3=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Numeric test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> p3<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;Python Partial elapsed time %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p1+p2+p3<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
t1+=p1<span style="color: #66cc66;">;</span>t2+=p2<span style="color: #66cc66;">;</span>t3+=p3
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>3. Starting Python tests...&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2, r3, xstr = strings_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
p1=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Strings test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> p1<span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2, r3, r4, r5 = arrays_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">50</span>, xstr<span style="color: black;">&#41;</span>
p2=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;Arrays test  - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> p2<span style="color: black;">&#41;</span>
&nbsp;
stime = <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
r1, r2 = nums_test<span style="color: black;">&#40;</span><span style="color: #ff4500;">500</span><span style="color: black;">&#41;</span>
p3=<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - stime
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Numeric test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> p3<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;Python Partial elapsed time %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>p1+p2+p3<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
t1+=p1<span style="color: #66cc66;">;</span>t2+=p2<span style="color: #66cc66;">;</span>t3+=p3
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-------------------------------------&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Average Strings test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>t1/<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Average Arrays test  - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>t2/<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Average Numeric test - Elapsed %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>t3/<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Python Average elapsed time %.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>t1/<span style="color: #ff4500;">3</span>+t2/<span style="color: #ff4500;">3</span>+t3/<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2010/01/ruby-vs-ruby-vs-python-vs-python-2/feed/?lang=en</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Manage routing errors</title>
		<link>http://mastrodonato.info/index.php/2009/11/gestire-instradamenti-errati-2/?lang=en</link>
		<comments>http://mastrodonato.info/index.php/2009/11/gestire-instradamenti-errati-2/?lang=en#comments</comments>
		<pubDate>Fri, 20 Nov 2009 11:31:12 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby on Rails @en]]></category>
		<category><![CDATA[Routes]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=371</guid>
		<description><![CDATA[Our application can only respond to certain requests, of course, to those that we have anticipated. For all other generates an error depending on the type: Routing errors Errors inside the controller Routing errors Small premise: In a traditional application, to retrieve a nonexistent page from the web server, we received a code 404. In [...]]]></description>
			<content:encoded><![CDATA[<p>Our application can only respond to certain requests, of course, to those that we have anticipated. For all other generates an error depending on the type:</p>
<ul>
<li>Routing errors</li>
<li>Errors inside the controller</li>
</ul>
<h3>Routing errors</h3>
<p><strong>Small premise</strong>: In a traditional application, to retrieve a nonexistent page from the web server, we received a code 404. In a framework with a pattern mvc instead, requests through controllers and this allows us to define the behavior. The requests are performed according to the routing rules and in Rails for convention, all the controllers can be addressed. If we open the file routes.rb, at the bottom we discover why:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#configroutes.rb</span>
map.<span style="color:#9900CC;">connect</span> <span style="color:#996600;">':controller/:action/:id'</span>
map.<span style="color:#9900CC;">connect</span> <span style="color:#996600;">':controller/:action/:id.:format'</span></pre></div></div>

<p>To ensure that only certain controllers can be routed, just delete those two lines at the bottom of the file that only defines the form of the request, leaving only the explicit routes. End of the preface.</p>
<p>If there isn&#8217;t a route, this generates a <strong>routing error</strong> because Rails does not know what we want. To handle this event, simply add one last road before you generate the error, which will inform them that has not found other valid roads:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#configroutes.rb</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#at the bottom</span>
map.<span style="color:#9900CC;">catch_all</span> <span style="color:#996600;">&quot;*anything&quot;</span> , <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;home&quot;</span> , <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;unknown_request&quot;</span></pre></div></div>

<p>We saw how catch the not handled routes and directs it to the controller where we want to manage it, for that example at home controller using the method unknown_request:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#appcontrollershome_controller.rb</span>
<span style="color:#9966CC; font-weight:bold;">def</span> unknown_request
  respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:error</span><span style="color:#006600; font-weight:bold;">&#93;</span> = I18n.<span style="color:#9900CC;">t</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:unknown_request</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      redirect_to root_path
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span>  render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:root <span style="color:#006600; font-weight:bold;">=&gt;</span> I18n.<span style="color:#9900CC;">t</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:unknown_request</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:unprocessable_entity</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h3>Errors inside the controller</h3>
<p>If the controller has been addressed, does not mean that the request is correct, because each has a series of operations that must exist and must be able to operate on the resource and manage the exceptions raised by the database.</p>
<p>Since version 2.0 of Rails, we can use &#8220;rescue_from&#8221; in the controller:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#appcontrollersmy_controller.rb</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> MyController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  rescue_from <span style="color:#6666ff; font-weight:bold;">ActionController::UnknownAction</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:action_not_found</span>
  rescue_from <span style="color:#6666ff; font-weight:bold;">ActiveRecord::RecordNotFound</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:record_not_found</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> action_not_found
    render <span style="color:#996600;">'shared/action_not_found'</span>, <span style="color:#ff3333; font-weight:bold;">:layout</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">404</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> record_not_found
    render <span style="color:#996600;">'shared/record_not_found'</span>, <span style="color:#ff3333; font-weight:bold;">:layout</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">404</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Here are the views:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">#appviewssharedaction _not_found.html.erb
&lt;h2&gt;Sorry, the action doesn't exists!&lt;/h2&gt;
&nbsp;
#appviewssharedrecord_not_found.html.erb
&lt;h2&gt;Sorry, the record doesn't exists!&lt;/h2&gt;</pre></div></div>

<p>Another more concise way is to pass a proc as a parameter to &#8220;rescue_from&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#appcontrollersmy_controller.rb</span>
rescue_from<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::RecordNotFound</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> render <span style="color:#996600;">'shared/record_not_found'</span>, <span style="color:#ff3333; font-weight:bold;">:layout</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">404</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/11/gestire-instradamenti-errati-2/feed/?lang=en</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comparison script languages for the fractal geometry</title>
		<link>http://mastrodonato.info/index.php/2009/08/comparison-script-languages-for-the-fractal-geometry/?lang=en</link>
		<comments>http://mastrodonato.info/index.php/2009/08/comparison-script-languages-for-the-fractal-geometry/?lang=en#comments</comments>
		<pubDate>Mon, 24 Aug 2009 09:58:28 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[IronRuby .NET @en]]></category>
		<category><![CDATA[JRuby @en]]></category>
		<category><![CDATA[Ruby @en]]></category>
		<category><![CDATA[Benchmark]]></category>
		<category><![CDATA[Frattali]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[Mandelbrot]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=339</guid>
		<description><![CDATA[This article will compare the latest incarnations of Ruby, with the latest in Python, Groovy, PHP, Lua, Perl and Java too, to have a comparison with a pre-compiled language. We will see, how scripting languages behave if applied to fractal geometry, more precisely an family Mandelbrot algorithm. Browsing on the net, I found a comparison [...]]]></description>
			<content:encoded><![CDATA[<p>This article will compare the latest incarnations of Ruby, with the latest in Python, Groovy, PHP, Lua, Perl and Java too, to have a comparison with a pre-compiled language. We will see, how scripting languages behave if applied to fractal geometry, more precisely an family Mandelbrot algorithm.<br />
Browsing on the net, I found a <a href="http://www.timestretch.com/FractalBenchmark.html#4d9e0f15fc1b42420cf3f778c3cccad7"> comparison</a> very interesting but a bit dated, dates back more than two years ago. Since then things have changed and I took advantage to make an update, not including all of those languages but only for more known. This is an opportunity to compare Ruby and Python versions even in their Java and. NET, an intention that I had since long time.</p>
<p>Using a fractal as a convenient benchmark plus: if an attempt to optimize not be successful if it has been the evidence and being drawn in real time, you can feel the speed. The fractal is drawn in ASCII also because the use of external libraries would have drugged the outcome.</p>
<pre>
                                       *
                                       *
                                       *
                                       *
                                       *
                                      ***
                                     *****
                                     *****
                                      ***
                                       *
                                   *********
                                 *************
                                ***************
                             *********************
                             *********************
                              *******************
                              *******************
                              *******************
                              *******************
                            ***********************
                              *******************
                              *******************
                             *********************
                              *******************
                              *******************
                               *****************
                                ***************
                                 *************
                                   *********
                                       *
                                ***************
                            ***********************
                         * ************************* *
                         *****************************
                      * ******************************* *
                       *********************************
                      ***********************************
                    ***************************************
               *** ***************************************** ***
               *************************************************
                ***********************************************
                 *********************************************
                 *********************************************
                ***********************************************
                ***********************************************
              ***************************************************
               *************************************************
               *************************************************
              ***************************************************
              ***************************************************
         *    ***************************************************    *
       *****  ***************************************************  *****
       ****** *************************************************** ******
      ******* *************************************************** *******
    ***********************************************************************
    ********* *************************************************** *********
       ****** *************************************************** ******
       *****  ***************************************************  *****
              ***************************************************
              ***************************************************
              ***************************************************
              ***************************************************
               *************************************************
               *************************************************
              ***************************************************
                ***********************************************
                ***********************************************
                  *******************************************
                   *****************************************
                 *********************************************
                **** ****************** ****************** ****
                 ***  ****************   ****************  ***
                  *    **************     **************    *
                         ***********       ***********
                         **  *****           *****  **
                          *   *                 *   *
</pre>
<p>This is the system for the test:<br />
Dell Inspiron 9400, Centrino Duo, T7200 @ 2Ghz 4Mb Cache L1, Ram 2Gb @ 667Mhz<br />
Windows XP pro SP3<br />
Java 6 update 15<br />
Microsoft .NET 3.5 SP1</p>
<p>These are the performance results obtained from an average of five runs, took after have executed some void attempts (I have not trusted the VM startup):</p>
<pre>
Language      Time (in seconds)  n times slower tha java
_____________________________________________________________
Java 6 update 15    0,153
Lua 5.1.4           0,815	           5x
Php 5.3.0           2,083	          14x
Python 2.6.2        2,269 	          15x
Python 3.1.1        1,566 	          10x
Jython 2.5.0        2,850 	          19x
Jruby 1.3.1         2,466 	          16x
Groovy 1.6.3        6,491 	          42x
Ruby 1.9.1 p129	    2,688 	          18x
Ruby 1.8.6 p368	    6,863 	          45x
Ruby 1.8.6 p111	    9,709 	          63x
IronRuby 0.9.0	    6,038 	          39x
IronPyhon 2.0.2     0,978 	           6x
Perl 5.10.0         2,722 	          18x
</pre>
<p>This is the chart, of course <strong>lower values indicate better performance</strong></p>
<p><img src="http://mastrodonato.info/wp-content/uploads/2009/08/chart2_537.png" alt="Chart" title="Chart" width="537" height="457" class="aligncenter size-full wp-image-262" /></p>
<p>These are the scripts used to generate the fractal, the originals was good, I have only done a few simple changes to run Python 3.1 or slightly improve the already excellent readability in Ruby and Lua.</p>
<p><strong>Java</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// by Erik Wrenholt</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Bench1
<span style="color: #009900;">&#123;</span>  
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> BAILOUT <span style="color: #339933;">=</span> <span style="color: #cc66cc;">16</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> MAX_ITERATIONS <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> iterate<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span> x, <span style="color: #000066; font-weight: bold;">float</span> y<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">float</span> cr <span style="color: #339933;">=</span> y<span style="color: #339933;">-</span>0.5f<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">float</span> ci <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">float</span> zi <span style="color: #339933;">=</span> 0.0f<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">float</span> zr <span style="color: #339933;">=</span> 0.0f<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			i<span style="color: #339933;">++;</span>
			<span style="color: #000066; font-weight: bold;">float</span> temp <span style="color: #339933;">=</span> zr <span style="color: #339933;">*</span> zi<span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">float</span> zr2 <span style="color: #339933;">=</span> zr <span style="color: #339933;">*</span> zr<span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">float</span> zi2 <span style="color: #339933;">=</span> zi <span style="color: #339933;">*</span> zi<span style="color: #339933;">;</span>
			zr <span style="color: #339933;">=</span> zr2 <span style="color: #339933;">-</span> zi2 <span style="color: #339933;">+</span> cr<span style="color: #339933;">;</span>
			zi <span style="color: #339933;">=</span> temp <span style="color: #339933;">+</span> temp <span style="color: #339933;">+</span> ci<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>zi2 <span style="color: #339933;">+</span> zr2 <span style="color: #339933;">&gt;</span> BAILOUT<span style="color: #009900;">&#41;</span>
				<span style="color: #000000; font-weight: bold;">return</span> i<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&gt;</span> MAX_ITERATIONS<span style="color: #009900;">&#41;</span>
				<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Date</span> d1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</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;">int</span> x,y<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>y <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">39</span><span style="color: #339933;">;</span> y <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">39</span><span style="color: #339933;">;</span> y<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">39</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">39</span><span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>iterate<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">/</span>40.0f,y<span style="color: #339933;">/</span>40.0f<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> 
					<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;*&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">else</span>
					<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #003399;">Date</span> d2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</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;">long</span> diff <span style="color: #339933;">=</span> d2.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> d1.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nJava Elapsed &quot;</span> <span style="color: #339933;">+</span> diff<span style="color: #339933;">/</span>1000.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Lua</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- By Erik Wrenholt</span>
&nbsp;
<span style="color: #b1b100;">local</span> BAILOUT <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">16</span>
<span style="color: #b1b100;">local</span> MAX_ITERATIONS <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1000</span>
&nbsp;
<span style="color: #b1b100;">function</span> iterate<span style="color: #66cc66;">&#40;</span>x,y<span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #b1b100;">local</span> cr <span style="color: #66cc66;">=</span> y-<span style="color: #cc66cc;">0.5</span>
  <span style="color: #b1b100;">local</span> ci <span style="color: #66cc66;">=</span> x
  <span style="color: #b1b100;">local</span> zi <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0.0</span>
  <span style="color: #b1b100;">local</span> zr <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0.0</span>
  <span style="color: #b1b100;">local</span> i <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
&nbsp;
  <span style="color: #b1b100;">while</span> <span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">do</span>
    i <span style="color: #66cc66;">=</span> i+<span style="color: #cc66cc;">1</span>
    <span style="color: #b1b100;">local</span> temp <span style="color: #66cc66;">=</span> zr <span style="color: #66cc66;">*</span> zi
    <span style="color: #b1b100;">local</span> zr2 <span style="color: #66cc66;">=</span> zr<span style="color: #66cc66;">*</span>zr
    <span style="color: #b1b100;">local</span> zi2 <span style="color: #66cc66;">=</span> zi<span style="color: #66cc66;">*</span>zi
    zr <span style="color: #66cc66;">=</span> zr2-zi2+cr
    zi <span style="color: #66cc66;">=</span> temp+temp+ci
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>zi2+zr2 <span style="color: #66cc66;">&gt;</span> BAILOUT<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">then</span>
      <span style="color: #b1b100;">return</span> i
    <span style="color: #b1b100;">end</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>i <span style="color: #66cc66;">&gt;</span> MAX_ITERATIONS<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">then</span>
      <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>
    <span style="color: #b1b100;">end</span>
  <span style="color: #b1b100;">end</span>
<span style="color: #b1b100;">end</span>
&nbsp;
<span style="color: #b1b100;">function</span> bench1<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">local</span> t <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;">for</span> y <span style="color: #66cc66;">=</span> -<span style="color: #cc66cc;">39</span>, <span style="color: #cc66cc;">38</span> <span style="color: #b1b100;">do</span>
    <span style="color: #b1b100;">for</span> x <span style="color: #66cc66;">=</span> -<span style="color: #cc66cc;">39</span>, <span style="color: #cc66cc;">38</span> <span style="color: #b1b100;">do</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>iterate<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">40.0</span>, y<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">then</span> <span style="color: #b1b100;">io.write</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">io.write</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">end</span>
    <span style="color: #b1b100;">end</span>
    <span style="color: #b1b100;">io.write</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;n&quot;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">end</span>
  <span style="color: #b1b100;">io.write</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">string.format</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Time Elapsed %.3fn&quot;</span>, <span style="color: #b1b100;">os.clock</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> - t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">end</span>
&nbsp;
bench1<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p><strong>Php</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;BAILOUT&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MAX_ITERATIONS&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Bench1
<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> Bench1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$d1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">39</span><span style="color: #339933;">;</span> <span style="color: #000088;">$y</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">39</span><span style="color: #339933;">;</span> <span style="color: #000088;">$y</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">39</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">39</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">iterate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">/</span><span style="color:#800080;">40.0</span><span style="color: #339933;">,</span><span style="color: #000088;">$y</span><span style="color: #339933;">/</span><span style="color:#800080;">40.0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> 
					<span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;*&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">else</span>
					<span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</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: #000088;">$d2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$diff</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$d2</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$d1</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nPHP Elapsed <span style="color: #009933; font-weight: bold;">%0.3f</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$diff</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> iterate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">,</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$cr</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$y</span><span style="color: #339933;">-</span><span style="color:#800080;">0.5</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$ci</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$zi</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$zr</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">while</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: #009900;">&#123;</span>
			<span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
			<span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$zr</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$zi</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$zr2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$zr</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$zr</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$zi2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$zi</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$zi</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$zr</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$zr2</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$zi2</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$cr</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$zi</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$temp</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$temp</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$ci</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$zi2</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$zr2</span> <span style="color: #339933;">&gt;</span> BAILOUT<span style="color: #009900;">&#41;</span>
				<span style="color: #b1b100;">return</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">&gt;</span> MAX_ITERATIONS<span style="color: #009900;">&#41;</span>
				<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">new</span> Bench1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Python</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>, <span style="color: #dc143c;">time</span>
stdout = <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>
&nbsp;
BAILOUT = <span style="color: #ff4500;">16</span>
MAX_ITERATIONS = <span style="color: #ff4500;">1000</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Bench1:
  <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">'Rendering...'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> y <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>-<span style="color: #ff4500;">39</span>, <span style="color: #ff4500;">39</span><span style="color: black;">&#41;</span>:
      stdout.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'n'</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>-<span style="color: #ff4500;">39</span>, <span style="color: #ff4500;">39</span><span style="color: black;">&#41;</span>:
        i = <span style="color: #008000;">self</span>.<span style="color: black;">start</span><span style="color: black;">&#40;</span>x/<span style="color: #ff4500;">40.0</span>, y/<span style="color: #ff4500;">40.0</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> i == <span style="color: #ff4500;">0</span>:
          stdout.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'*'</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
          stdout.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">' '</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> start<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, x, y<span style="color: black;">&#41;</span>:
    cr = y - <span style="color: #ff4500;">0.5</span>
    ci = x
    zi = zr = <span style="color: #ff4500;">0.0</span>
    i = <span style="color: #ff4500;">0</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
      i += <span style="color: #ff4500;">1</span>
      temp = zr <span style="color: #66cc66;">*</span> zi
      zr2 = zr <span style="color: #66cc66;">*</span> zr
      zi2 = zi <span style="color: #66cc66;">*</span> zi
      zr = zr2 - zi2 + cr
      zi = temp + temp + ci
&nbsp;
      <span style="color: #ff7700;font-weight:bold;">if</span> zi2 + zr2 <span style="color: #66cc66;">&gt;</span> BAILOUT:
        <span style="color: #ff7700;font-weight:bold;">return</span> i
      <span style="color: #ff7700;font-weight:bold;">if</span> i <span style="color: #66cc66;">&gt;</span> MAX_ITERATIONS:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
&nbsp;
t = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
Bench1<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">'nPython Elapsed %.3f'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - t<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><strong>Groovy</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Created By Marco Mastrodonato 22/09/2009</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Bench1<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">int</span> BAILOUT <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">16</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">int</span> MAX_ITERATIONS <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1000</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> Bench1<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #993399;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Rendering...&quot;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>y <span style="color: #b1b100;">in</span> <span style="color: #66cc66;">-</span>39..39<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
            <span style="color: #993399;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>x <span style="color: #b1b100;">in</span> <span style="color: #66cc66;">-</span>39..39<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>iterate<span style="color: #66cc66;">&#40;</span>x/<span style="color: #cc66cc;">40.0</span>, y/<span style="color: #cc66cc;">40.0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
                    <span style="color: #993399;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #993399;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#125;</span>
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> iterate<span style="color: #66cc66;">&#40;</span>x,y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #993333;">float</span> cr <span style="color: #66cc66;">=</span> y<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">0.5</span>
        <span style="color: #993333;">float</span> ci <span style="color: #66cc66;">=</span> x
        <span style="color: #993333;">float</span> zi <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0.0</span>
        <span style="color: #993333;">float</span> zr <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0.0</span>
        <span style="color: #000000; font-weight: bold;">def</span> i <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
        <span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
            i <span style="color: #66cc66;">+=</span> <span style="color: #cc66cc;">1</span>
            <span style="color: #993333;">float</span> temp <span style="color: #66cc66;">=</span> zr <span style="color: #66cc66;">*</span> zi
            <span style="color: #993333;">float</span> zr2 <span style="color: #66cc66;">=</span> zr <span style="color: #66cc66;">*</span> zr
            <span style="color: #993333;">float</span> zi2 <span style="color: #66cc66;">=</span> zi <span style="color: #66cc66;">*</span> zi
            zr <span style="color: #66cc66;">=</span> zr2 <span style="color: #66cc66;">-</span> zi2 <span style="color: #66cc66;">+</span> cr
            zi <span style="color: #66cc66;">=</span> temp <span style="color: #66cc66;">+</span> temp <span style="color: #66cc66;">+</span> ci
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>zi2 <span style="color: #66cc66;">+</span> zr2 <span style="color: #66cc66;">&gt;</span> BAILOUT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> 
                <span style="color: #000000; font-weight: bold;">return</span> i
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>i <span style="color: #66cc66;">&gt;</span> MAX_ITERATIONS<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> 
                <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span>
            <span style="color: #66cc66;">&#125;</span> 
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
time1 <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">time</span>
<span style="color: #000000; font-weight: bold;">new</span> Bench1<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
time2 <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">time</span>
<span style="color: #993333;">float</span> elapsed <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>time2 <span style="color: #66cc66;">-</span> time1<span style="color: #66cc66;">&#41;</span>/<span style="color: #cc66cc;">1000</span>
<span style="color: #993399;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;nGroovy Elapsed ${elapsed}&quot;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p><strong>Ruby</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">BAILOUT = <span style="color:#006666;">16</span>
MAX_ITERATIONS = <span style="color:#006666;">1000</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Bench1
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Rendering...&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">for</span> y <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006600; font-weight:bold;">-</span>39..39
      <span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;n&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">for</span> x <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006600; font-weight:bold;">-</span>39..39
        i = iterate x<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">40.0</span>, y<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">40.0</span>
        <span style="color:#9966CC; font-weight:bold;">if</span> i == <span style="color:#006666;">0</span> <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;*&quot;</span> <span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot; &quot;</span> <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> iterate<span style="color:#006600; font-weight:bold;">&#40;</span>x,y<span style="color:#006600; font-weight:bold;">&#41;</span>
    cr = y<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">0.5</span>
    ci = x
    zi = zr = <span style="color:#006666;">0.0</span>
    i = <span style="color:#006666;">0</span>
    <span style="color:#9966CC; font-weight:bold;">while</span> <span style="color:#0000FF; font-weight:bold;">true</span>
      i <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
      temp = zr <span style="color:#006600; font-weight:bold;">*</span> zi
      zr2 = zr <span style="color:#006600; font-weight:bold;">*</span> zr
      zi2 = zi <span style="color:#006600; font-weight:bold;">*</span> zi
      zr = zr2 <span style="color:#006600; font-weight:bold;">-</span> zi2 <span style="color:#006600; font-weight:bold;">+</span> cr
      zi = temp <span style="color:#006600; font-weight:bold;">+</span> temp <span style="color:#006600; font-weight:bold;">+</span> ci
      <span style="color:#0000FF; font-weight:bold;">return</span> i <span style="color:#9966CC; font-weight:bold;">if</span> zi2 <span style="color:#006600; font-weight:bold;">+</span> zr2 <span style="color:#006600; font-weight:bold;">&gt;</span> BAILOUT
      <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006666;">0</span> <span style="color:#9966CC; font-weight:bold;">if</span> i <span style="color:#006600; font-weight:bold;">&gt;</span> MAX_ITERATIONS
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
Bench1.<span style="color:#9900CC;">new</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;nRuby Elapsed %.3f&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> time<span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p><strong>Perl</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Ported from C to Perl by Anders Bergh &lt;anders1@gmail.com&gt;</span>
&nbsp;
<span style="color: #0000ff;">$BAILOUT</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">16</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$MAX_ITERATIONS</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$begin</span> <span style="color: #339933;">=</span> <span style="color: #000066;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> mandelbrot <span style="color: #009900;">&#123;</span>
       <span style="color: #000066;">local</span> <span style="color: #0000ff;">$x</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
       <span style="color: #000066;">local</span> <span style="color: #0000ff;">$y</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
       <span style="color: #000066;">local</span> <span style="color: #0000ff;">$cr</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$y</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">0.5</span><span style="color: #339933;">;</span>
       <span style="color: #000066;">local</span> <span style="color: #0000ff;">$ci</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$x</span><span style="color: #339933;">;</span>
       <span style="color: #000066;">local</span> <span style="color: #0000ff;">$zi</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.0</span><span style="color: #339933;">;</span>
       <span style="color: #000066;">local</span> <span style="color: #0000ff;">$zr</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.0</span><span style="color: #339933;">;</span>
       <span style="color: #000066;">local</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
       <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
       <span style="color: #009900;">&#123;</span>
               <span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
               <span style="color: #000066;">local</span> <span style="color: #0000ff;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$zr</span> <span style="color: #339933;">*</span> <span style="color: #0000ff;">$zi</span><span style="color: #339933;">;</span>
               <span style="color: #000066;">local</span> <span style="color: #0000ff;">$zr2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$zr</span> <span style="color: #339933;">*</span> <span style="color: #0000ff;">$zr</span><span style="color: #339933;">;</span>
               <span style="color: #000066;">local</span> <span style="color: #0000ff;">$zi2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$zi</span> <span style="color: #339933;">*</span> <span style="color: #0000ff;">$zi</span><span style="color: #339933;">;</span>
               <span style="color: #0000ff;">$zr</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$zr2</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$zi2</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">$cr</span><span style="color: #339933;">;</span>
               <span style="color: #0000ff;">$zi</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$temp</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">$temp</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">$ci</span><span style="color: #339933;">;</span>
               <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$zi2</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">$zr2</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000ff;">$BAILOUT</span><span style="color: #009900;">&#41;</span>
               <span style="color: #009900;">&#123;</span>
                       <span style="color: #000066;">return</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">;</span>
               <span style="color: #009900;">&#125;</span>
               <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000ff;">$MAX_ITERATIONS</span><span style="color: #009900;">&#41;</span>
               <span style="color: #009900;">&#123;</span>
                       <span style="color: #000066;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
               <span style="color: #009900;">&#125;</span>
       <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$y</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">39</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$y</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">39</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$y</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
       <span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;n&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$x</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">39</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$x</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">39</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
       <span style="color: #009900;">&#123;</span>
               <span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> mandelbrot<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$x</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">40.0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$y</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">40.0</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: #0000ff;">$i</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
               <span style="color: #009900;">&#123;</span>
                       <span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #009900;">&#125;</span>
               <span style="color: #b1b100;">else</span>
               <span style="color: #009900;">&#123;</span>
                       <span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; &quot;</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: #009900;">&#125;</span>
<span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;n&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$end</span> <span style="color: #339933;">=</span> <span style="color: #000066;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$begin</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">printf</span> <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Perl Elapsed %.3fn&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$end</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Comments:</h3>
<p>The speed of Lua isn&#8217;t a news: only 5 times slower than compiled Java code, the best result. Its simplicity is its strength, maybe that&#8217;s what makes it so fast? It was adopted by Blizzard in the videogame &#8220;World of Warcraft&#8221; and if they did so, with no doubt, there is a reason. It is not object-oriented, does not natively support objects even if there is a project <a href="http://loop.luaforge.net/"> LOOP</a> that extends this programming model.<br />
PHP and Perl does not need comments.<br />
Among the C version of Ruby and Python is clearly faster the second. The fair comparison would be the Rb1.8.6 with Py2.6.2 and Rb1.9.1 with Py3.1.1.<br />
The challenge between the versions that uses the Java VM: Groovy, Jython and JRuby, sees the last as the winner. Groovy is far behind as performance in this test but the biggest question I have is: who is it for? As syntax is not bad but, imho, Ruby is even more fluent and then have that rake that is so convenient for many things.<br />
The challenge between the .NET versions sees IronPython incredibly forward! But what they put in, the dynamite? It will be very interesting to examine the new ASP.NET MVC framework recently arrived at version 1 and that will be included in the framework. Net 4, there are projects for both IronRuby IronPython.<br />
If this article was interesting, perhaps you can find something else through the advertisements of my sponsor, is in the right column, thanks!<br />
Bye!</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/08/comparison-script-languages-for-the-fractal-geometry/feed/?lang=en</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Five rubies in the multicore challenge</title>
		<link>http://mastrodonato.info/index.php/2009/08/five-rubies-in-the-multicore-challenge/?lang=en</link>
		<comments>http://mastrodonato.info/index.php/2009/08/five-rubies-in-the-multicore-challenge/?lang=en#comments</comments>
		<pubDate>Mon, 24 Aug 2009 09:07:33 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[IronRuby .NET @en]]></category>
		<category><![CDATA[JRuby @en]]></category>
		<category><![CDATA[Ruby @en]]></category>
		<category><![CDATA[Benchmark]]></category>
		<category><![CDATA[multicore]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=333</guid>
		<description><![CDATA[The system on which I performed the test is a laptop: Dell Inspiron 9400 with Centrino Duo, Intel T7200 4MB Cache 2GHz (166&#215;12) 2GB Ram 667Mhz. This is a physical system Windows XP Pro SP3. The purpose: to check the exploitation of more processor cores (two in my case) by comparing a single process with [...]]]></description>
			<content:encoded><![CDATA[<p>The system on which I performed the test is a laptop: Dell Inspiron 9400 with Centrino Duo, Intel T7200 4MB Cache 2GHz (166&#215;12) 2GB Ram 667Mhz. This is a physical system Windows XP Pro SP3.</p>
<p>The <strong>purpose</strong>: to check the exploitation of more processor cores (two in my case) by comparing a single process with a double execution simultaneously.</p>
<p>To do this I used the string&#8217;s bench used in a <a href="http://mastrodonato.info/index.php/2009/08/best-practise-with-four-rubys-interpreters/?lang=en"> previous article </a>.<br />
It must be remembered that the implementation of the dual process, everyone must share the CPU with the operating system (the load is always 100%) while in the single test that does not happen, a core is dedicated only to the test. For this reason, the operating system didn&#8217;t run antivirus or other heavy processes.</p>
<p>Let&#8217;s start with the oldest interpreter:</p>
<p>
<strong>Ruby 1.8.6 patch 111</strong></p>
<pre>
C:LavoroProgettiTestBench>ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

C:LavoroProgettiTestBench>ruby bench_str.rb
                           user     system      total        real
Concat 1.000.000:
+                      6.641000   0.000000   6.641000 (  6.640000)
<<                     5.344000   0.000000   5.344000 (  5.375000)
#{}                    6.078000   0.000000   6.078000 (  6.078000)
Add 100.000:
+=                     3.547000   2.969000   6.516000 (  6.579000)
<<                     0.062000   0.000000   0.062000 (  0.062000)
a = a + '.'            3.000000   3.422000   6.422000 (  6.547000)
#{}                    4.531000   1.906000   6.437000 (  6.516000)
Other 100.000:
* 100:                 0.500000   0.063000   0.563000 (  0.562000)
capitalize:            1.719000   0.078000   1.797000 (  1.797000)
upcase:                4.906000   0.140000   5.046000 (  5.047000)
chomp:                 0.266000   0.063000   0.329000 (  0.328000)
include:               1.234000   0.000000   1.234000 (  1.234000)
index:                 1.235000   0.000000   1.235000 (  1.250000)
sub:                   0.875000   0.094000   0.969000 (  0.969000)
gsub:                 17.453000   0.562000  18.015000 ( 18.047000)
[x..y]:                0.547000   0.016000   0.563000 (  0.562000)
slice:                 0.562000   0.000000   0.562000 (  0.563000)
strip:                 0.156000   0.000000   0.156000 (  0.156000)
Each:                 12.282000   0.078000  12.360000 ( 12.390000)
Cast 1.000.000:
.to_i:                 0.437000   0.000000   0.437000 (  0.438000)
.to_sym:               0.531000   0.000000   0.531000 (  0.531000)
split:                 9.047000   0.140000   9.187000 (  9.235000)
--- Total:            80.953000   9.531000  90.484000 ( 90.906000)
</pre>
<div id="attachment_152" class="wp-caption aligncenter" style="width: 249px"><a href="http://mastrodonato.info/wp-content/uploads/2009/08/bench_str_186p111_1.png"><img src="http://mastrodonato.info/wp-content/uploads/2009/08/bench_str_186p111_1-239x300.png" alt="Ruby 1.8.6 patch 111" title="bench_str_186p111_1" width="239" height="300" class="size-medium wp-image-152" /></a><p class="wp-caption-text">Ruby 1.8.6 patch 111</p></div>
<p>
Single thread:</p>
<pre>
--- Total:            80.953000   9.531000  90.484000 ( 90.906000)
</pre>
<p>Double simultaneous execution:</p>
<pre>
CORE1:
--- Total:            87.063000  19.531000 106.594000 (107.923000)

CORE2:
--- Total:            91.344000  18.375000 109.719000 (110.125000)
</pre>
<p>This is the decrease of the double simultaneous execution:<br />
Decrease by 10%<br />
Real decrease of 20%</p>
<p><strong>Ruby 1.8.6 patch 368</strong></p>
<pre>
C:LavoroProgettiTestBench>ruby -v
ruby 1.8.6 (2009-03-31 patchlevel 368) [i386-mingw32]

C:LavoroProgettiTestBench>ruby bench_str.rb
                           user     system      total        real
Concat 1.000.000:
+                      4.828000   0.000000   4.828000 (  4.828125)
<<                     3.938000   0.000000   3.938000 (  3.953125)
#{}                    4.719000   0.016000   4.735000 (  4.750000)
Add 100.000:
+=                     3.687000   2.719000   6.406000 (  6.468750)
<<                     0.047000   0.000000   0.047000 (  0.046875)
a = a + '.'            3.422000   3.000000   6.422000 (  6.468750)
#{}                    4.797000   1.109000   5.906000 (  5.906250)
Other 100.000:
* 100:                 0.328000   0.094000   0.422000 (  0.421875)
capitalize:            0.890000   0.078000   0.968000 (  1.000000)
upcase:                3.469000   0.109000   3.578000 (  3.578125)
chomp:                 0.235000   0.078000   0.313000 (  0.312500)
include:               0.796000   0.000000   0.796000 (  0.796875)
index:                 0.797000   0.000000   0.797000 (  0.796875)
sub:                   0.750000   0.172000   0.922000 (  0.921875)
gsub:                 20.860000   0.531000  21.391000 ( 21.421875)
[x..y]:                0.437000   0.016000   0.453000 (  0.453125)
slice:                 0.438000   0.000000   0.438000 (  0.437500)
strip:                 0.109000   0.000000   0.109000 (  0.109375)
Each:                  9.078000   0.172000   9.250000 (  9.281250)
Cast 1.000.000:
.to_i:                 0.297000   0.000000   0.297000 (  0.296875)
.to_sym:               0.344000   0.000000   0.344000 (  0.343750)
split:                 7.375000   0.109000   7.484000 (  7.500000)
--- Total:            71.641000   8.203000  79.844000 ( 80.093750)
</pre>
<div id="attachment_153" class="wp-caption aligncenter" style="width: 249px"><a href="http://mastrodonato.info/wp-content/uploads/2009/08/bench_str_186p368_1.png"><img src="http://mastrodonato.info/wp-content/uploads/2009/08/bench_str_186p368_1-239x300.png" alt="Ruby 1.8.6 patch 368" title="bench_str_186p368_1" width="239" height="300" class="size-medium wp-image-153" /></a><p class="wp-caption-text">Ruby 1.8.6 patch 368</p></div>
<p>
Single thread:</p>
<pre>
--- Total:            71.641000   8.203000  79.844000 ( 80.093750)
</pre>
<p>Double simultaneous execution:</p>
<pre>
CORE1:
--- Total:            80.797000  17.750000  98.547000 ( 99.093750)
CORE2:
--- Total:            76.500000  19.375000  95.875000 ( 97.187500)
</pre>
<p>Decrease of 9,8%<br />
Real decrease of 22,5%</p>
<p><strong>Ruby 1.9.1</strong></p>
<pre>
C:LavoroProgettiTestBench>ruby -v
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-mingw32]

C:LavoroProgettiTestBench>ruby bench_str.rb
                           user     system      total        real
Concat 1.000.000:
+                      2.610000   0.000000   2.610000 (  2.609375)
<<                     2.125000   0.000000   2.125000 (  2.125000)
#{}                    2.796000   0.000000   2.796000 (  2.796875)
Add 100.000:
+=                     3.688000   2.875000   6.563000 (  6.609375)
<<                     0.031000   0.000000   0.031000 (  0.031250)
a = a + '.'            3.235000   3.265000   6.500000 (  6.578125)
#{}                    5.187000   2.266000   7.453000 (  7.546875)
Other 100.000:
* 100:                 0.203000   0.125000   0.328000 (  0.328125)
capitalize:            5.563000   0.047000   5.610000 (  5.656250)
upcase:                0.703000   0.031000   0.734000 (  0.734375)
chomp:                 0.219000   0.063000   0.282000 (  0.296875)
include:               0.156000   0.000000   0.156000 (  0.156250)
index:                 0.156000   0.000000   0.156000 (  0.156250)
sub:                   0.500000   0.093000   0.593000 (  0.593750)
gsub:                  8.453000   0.266000   8.719000 (  8.718750)
[x..y]:                0.219000   0.000000   0.219000 (  0.218750)
slice:                 0.219000   0.000000   0.219000 (  0.218750)
strip:                 0.109000   0.000000   0.109000 (  0.109375)
Each:                  7.188000   0.141000   7.329000 (  7.328125)
Cast 1.000.000:
.to_i:                 0.234000   0.000000   0.234000 (  0.234375)
.to_sym:               0.391000   0.000000   0.391000 (  0.390625)
split:                 4.609000   0.000000   4.609000 (  4.609375)
--- Total:            48.594000   9.172000  57.766000 ( 58.046875)
</pre>
<div id="attachment_154" class="wp-caption aligncenter" style="width: 249px"><a href="http://mastrodonato.info/wp-content/uploads/2009/08/bench_str_191p129_1.png"><img src="http://mastrodonato.info/wp-content/uploads/2009/08/bench_str_191p129_1-239x300.png" alt="Ruby 1.9.1 patch 129" title="bench_str_191p129_1" width="239" height="300" class="size-medium wp-image-154" /></a><p class="wp-caption-text">Ruby 1.9.1 patch 129</p></div>
<p>
Single thread:</p>
<pre>
--- Total:            48.594000   9.172000  57.766000 ( 58.046875)
</pre>
<p>Double simultaneous execution:</p>
<pre>
CORE1:
--- Total:            56.345000  19.219000  75.564000 ( 76.312500)
CORE2:
--- Total:            52.109000  20.703000  72.812000 ( 74.156250)
</pre>
<p>Decrease of 11,5%<br />
Real decrease of 29,6%</p>
<p><strong>jRuby 1.3.1</strong></p>
<pre>
C:LavoroProgettiTestBench>jruby -v
jruby 1.3.1 (ruby 1.8.6p287) (2009-07-24 6586) (Java HotSpot(TM) Client VM 1.6.0
_15) [x86-java]

C:LavoroProgettiTestBench>jruby bench_str.rb
                           user     system      total        real
Concat 1.000.000:
+                      1.703000   0.000000   1.703000 (  1.672000)
<<                     1.375000   0.000000   1.375000 (  1.375000)
#{}                    0.625000   0.000000   0.625000 (  0.625000)
Add 100.000:
+=                     8.343000   0.000000   8.343000 (  8.343000)
<<                     0.031000   0.000000   0.031000 (  0.031000)
a = a + '.'            8.406000   0.000000   8.406000 (  8.406000)
#{}                   18.984000   0.000000  18.984000 ( 18.984000)
Other 100.000:
* 100:                 0.157000   0.000000   0.157000 (  0.157000)
capitalize:            0.937000   0.000000   0.937000 (  0.937000)
upcase:                1.703000   0.000000   1.703000 (  1.703000)
chomp:                 0.063000   0.000000   0.063000 (  0.063000)
include:               0.281000   0.000000   0.281000 (  0.281000)
index:                 0.313000   0.000000   0.313000 (  0.313000)
sub:                   0.437000   0.000000   0.437000 (  0.437000)
gsub:                  4.813000   0.000000   4.813000 (  4.813000)
[x..y]:                0.156000   0.000000   0.156000 (  0.156000)
slice:                 0.140000   0.000000   0.140000 (  0.140000)
strip:                 0.047000   0.000000   0.047000 (  0.047000)
Each:                  3.094000   0.000000   3.094000 (  3.094000)
Cast 1.000.000:
.to_i:                 0.609000   0.000000   0.609000 (  0.609000)
.to_sym:               0.266000   0.000000   0.266000 (  0.266000)
split:                 2.203000   0.000000   2.203000 (  2.203000)
--- Total:            54.686000   0.000000  54.686000 ( 54.655000)
</pre>
<div id="attachment_155" class="wp-caption aligncenter" style="width: 249px"><a href="http://mastrodonato.info/wp-content/uploads/2009/08/bench_str_jruby_131_186p287_1.png"><img src="http://mastrodonato.info/wp-content/uploads/2009/08/bench_str_jruby_131_186p287_1-239x300.png" alt="jRuby 1.3.1 =&gt; 1.8.6 p287" title="bench_str_jruby_131_186p287_1" width="239" height="300" class="size-medium wp-image-155" /></a><p class="wp-caption-text">jRuby 1.3.1 => 1.8.6 p287</p></div>
<p>
Single thread:</p>
<pre>
--- Total:            54.686000   0.000000  54.686000 ( 54.655000)
</pre>
<p>Double simultaneous execution:</p>
<pre>
CORE1:
--- Total:            65.170000   0.000000  65.170000 ( 64.920000)
CORE2:
--- Total:            64.234000   0.000000  64.234000 ( 64.000000)
</pre>
<p>Decrease of 17,2%<br />
Real decrease of 17,9%</p>
<p><strong>IronRuby 0.9.0</strong></p>
<pre>
C:LavoroProgettiTestBench>ir -v
IronRuby 0.9.0.0 on .NET 2.0.0.0

C:LavoroProgettiTestBench>ir bench_str.rb
                           user     system      total        real
Concat 1.000.000:
+                      3.562500   0.000000   3.562500 (  3.234375)
<<                     1.531250   0.000000   1.531250 (  1.531250)
#{}                    1.453125   0.015625   1.468750 (  1.453125)
Add 100.000:
+=                    54.656250  11.296875  65.953125 ( 66.171875)
<<                     0.031250   0.000000   0.031250 (  0.031250)
a = a + '.'           54.734375  11.718750  66.453125 ( 66.671875)
#{}                   55.609375  10.843750  66.453125 ( 66.609375)
Other 100.000:
* 100:                 0.671875   0.000000   0.671875 (  0.671875)
capitalize:            2.593750   0.000000   2.593750 (  2.609375)
upcase:                7.484375   0.000000   7.484375 (  7.500000)
chomp:                 0.250000   0.000000   0.250000 (  0.250000)
include:               5.968750   0.015625   5.984375 (  6.031250)
index:                 5.968750   0.031250   6.000000 (  6.031250)
sub:                   1.781250   0.015625   1.796875 (  1.781250)
gsub:                 12.203125   0.031250  12.234375 ( 12.281250)
[x..y]:                0.406250   0.000000   0.406250 (  0.390625)
slice:                 0.343750   0.000000   0.343750 (  0.343750)
strip:                 0.078125   0.000000   0.078125 (  0.078125)
Each:                 31.546875   0.312500  31.859375 ( 32.203125)
Cast 1.000.000:
.to_i:                 0.265625   0.000000   0.265625 (  0.250000)
.to_sym:               0.406250   0.015625   0.421875 (  0.421875)
split:                 5.515625   0.031250   5.546875 (  5.531250)
--- Total:           247.062500  34.328125 281.390625 (282.078125)

C:LavoroProgettiTestBench>
</pre>
<div id="attachment_156" class="wp-caption aligncenter" style="width: 249px"><a href="http://mastrodonato.info/wp-content/uploads/2009/08/bench_str_ironruby_090_1.png"><img src="http://mastrodonato.info/wp-content/uploads/2009/08/bench_str_ironruby_090_1-239x300.png" alt="IronRuby 0.9.0" title="bench_str_ironruby_090_1" width="239" height="300" class="size-medium wp-image-156" /></a><p class="wp-caption-text">IronRuby 0.9.0</p></div>
<p>
Single thread:</p>
<pre>
--- Total:           247.062500  34.328125 281.390625 (282.078125)
</pre>
<p>Double simultaneous execution:</p>
<pre>
CORE1:
--- Total:           315.531250  40.578125 356.109375 (359.500000)
CORE2:
--- Total:           300.484375  46.156250 346.640625 (350.265625)
</pre>
<p>Decrease of 24,5%<br />
Real decrease of 25,8%</p>
<h3>Summary</h3>
<p><strong>Ruby 1.8.6 patch 111</strong><br />
Decrease of 10%<br />
Real decrease of 20%</p>
<p><strong>Ruby 1.8.6 patch 368</strong><br />
Decrease of 9,8%<br />
Real decrease of 22,5%</p>
<p><strong>Ruby 1.9.1</strong><br />
Decrease of 11,5%<br />
Real decrease of 29,6%</p>
<p><strong>jRuby 1.3.1</strong><br />
Decrease of 17,2%<br />
Real decrease of 17,9%</p>
<p><strong>IronRuby 0.9.0</strong><br />
Decrease of 24,5%<br />
Real decrease of 25,8%</p>
<h3>Conclusions</h3>
<p>In this test, <strong>JRuby</strong> is the only one that has a uniform load on both cores and it does almost constantly for all the time. Only at the end moves towards a core but with more discretion. Also good performance with the two processes simultaneously.</p>
<p>Versions 1.8.6 and 1.9.1 are quite aligned and able to share the load solely on test "Add". The performance of the dual process would not be bad but creates problems for the system (at least mine) which prolongs the time.</p>
<p>IronRuby has some trouble with the strings already highlighted in a <a href="http://mastrodonato.info/index.php/2009/08/best-practise-with-four-rubys-interpreters/?lang=en">previous article</a>. Ignoring this issue, the second core was left unloaded for all the duration. With the second core free, I would have expected a less pronounced decrease in the test process twice but it did not happen, this is strange.</p>
<p>In a future article, I'd like to repeat the test on a server quad core.</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/08/five-rubies-in-the-multicore-challenge/feed/?lang=en</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FreeTTS, a java library used from jRuby on Rails</title>
		<link>http://mastrodonato.info/index.php/2009/08/freetts-a-java-library-used-from-jruby-on-rails/?lang=en</link>
		<comments>http://mastrodonato.info/index.php/2009/08/freetts-a-java-library-used-from-jruby-on-rails/?lang=en#comments</comments>
		<pubDate>Sun, 23 Aug 2009 14:54:40 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[JRuby @en]]></category>
		<category><![CDATA[Ruby on Rails @en]]></category>
		<category><![CDATA[FreeTTS]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[jRuby]]></category>
		<category><![CDATA[Text to speech]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=296</guid>
		<description><![CDATA[In this article we will see how to use the Java classes contained in a JAR file. For this purpose we use a nice open source library developed by Carnegie Mellon University: FreeTTS. The acronym TTS means Text To speech, makes it possible to transform a text into audio format. We&#8217;ll use it in a [...]]]></description>
			<content:encoded><![CDATA[<p>In this article we will see how to use the Java classes contained in a JAR file.<br />
For this purpose we use a nice open source library developed by Carnegie Mellon University: <a href="http://freetts.sourceforge.net/">FreeTTS</a>.<br />
The acronym TTS means Text To speech, makes it possible to transform a text into audio format. We&#8217;ll use it in a jRuby on Rails&#8217;s project to make us read the text we enter in the db.</p>
<p>In a <a href="http://mastrodonato.info/index.php/2009/08/setting-up-an-environment-for-jruby-on-rails/?lang=en/">previous article</a>, we have seen how to configure the environment, so let&#8217;s start creating the new application:</p>
<p><code><br />
C:>rails ProvaFreeTTS<br />
</code></p>
<p>In the lib folder from the root, create a sub folder freetts. Download the file <a href="http://sourceforge.net/projects/freetts/files/"> freetts-1.2.2<a href="http://sourceforge.net/projects/freetts/files/">freetts-1.2.2-bin.zip</a>, unpack the contents to a temporary folder, copy only the contents of the folder lib (jar files and JSAPI) into the dir just created: your_applibfreetts.</p>
<p>Now we create the jar&#8217;s interface to the jar:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#libfreetts.rb</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'freetts/freetts.jar'</span>
&nbsp;
import com.<span style="color:#9900CC;">sun</span>.<span style="color:#9900CC;">speech</span>.<span style="color:#9900CC;">freetts</span>.<span style="color:#9900CC;">Voice</span>
import com.<span style="color:#9900CC;">sun</span>.<span style="color:#9900CC;">speech</span>.<span style="color:#9900CC;">freetts</span>.<span style="color:#9900CC;">VoiceManager</span>
import com.<span style="color:#9900CC;">sun</span>.<span style="color:#9900CC;">speech</span>.<span style="color:#9900CC;">freetts</span>.<span style="color:#9900CC;">util</span>.<span style="color:#9900CC;">Utilities</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> FreeTTS
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@voice</span> = VoiceManager.<span style="color:#9900CC;">getInstance</span>.<span style="color:#9900CC;">getVoice</span><span style="color:#006600; font-weight:bold;">&#40;</span>Utilities.<span style="color:#9900CC;">getProperty</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;voice16kName&quot;</span>,<span style="color:#996600;">&quot;kevin16&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@voice</span>.<span style="color:#9900CC;">allocate</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> speak<span style="color:#006600; font-weight:bold;">&#40;</span>txt=<span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">unless</span> txt
    <span style="color:#0066ff; font-weight:bold;">@voice</span>.<span style="color:#9900CC;">speak</span> txt
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Let&#8217;s create a simple resource &#8220;<em>sentence</em>&#8221; with a single field &#8220;<em>body</em>&#8220;.</p>
<p><code><br />
C:ProvaFreeTTS>jruby script/generate scaffold sentence body:text<br />
</code></p>
<p>Now we create two new operations, as I explained in detail in a <a href="http://mastrodonato.info/index.php/2009/08/add-an-operation-to-a-resource-in-a-restful-system/?lang=en">previous article</a>.</p>
<p>We start from the controller by adding at the bottom :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;">#appcontrollerssentences_controller.rb</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> speak
    <span style="color:#0066ff; font-weight:bold;">@sentence</span> = Sentence.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:sentence</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'freetts'</span>
    tts = FreeTTS.<span style="color:#9900CC;">new</span>
    tts.<span style="color:#9900CC;">speak</span> <span style="color:#0066ff; font-weight:bold;">@sentence</span>.<span style="color:#9900CC;">body</span>
    render <span style="color:#006600; font-weight:bold;">&#40;</span>@sentence.<span style="color:#9900CC;">new_record</span>? ? <span style="color:#ff3333; font-weight:bold;">:new</span> : <span style="color:#ff3333; font-weight:bold;">:edit</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> read
    <span style="color:#0066ff; font-weight:bold;">@sentence</span> = Sentence.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'freetts'</span>
    tts = FreeTTS.<span style="color:#9900CC;">new</span>
    tts.<span style="color:#9900CC;">speak</span> <span style="color:#0066ff; font-weight:bold;">@sentence</span>.<span style="color:#9900CC;">body</span>
    redirect_to <span style="color:#ff3333; font-weight:bold;">:back</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now it&#8217;s the views turn.</p>
<p>We create a new partial where we&#8217;ll move into the resource&#8217;s data form, so to have only one form for every operation:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">#appviewssentence_sentence.html.erb
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span> form_for<span style="color:#006600; font-weight:bold;">&#40;</span>@sentence<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>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_area</span> <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span> <span style="color:#996600;">'Update'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
&lt;h2&gt;Preview&lt;/h2&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> form_for <span style="color:#0066ff; font-weight:bold;">@sentence</span>, <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> speak_sentences_path, <span style="color:#ff3333; font-weight:bold;">:method</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:put</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_area</span> <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span> <span style="color:#996600;">'Speak'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Add a clone of the form created by the scaffold but with a different action, to call the transaction speak.</p>
<p>Now modify the views created automatically:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">#appviewssentencenew.html.erb
&nbsp;
&lt;h1&gt;New sentence&lt;/h1&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= render <span style="color:#0066ff; font-weight:bold;">@sentence</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Back'</span>, sentences_path <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
#appviewssentenceedit.html.erb
&nbsp;
&lt;h1&gt;Editing sentence&lt;/h1&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= render <span style="color:#0066ff; font-weight:bold;">@sentence</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Show'</span>, <span style="color:#0066ff; font-weight:bold;">@sentence</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> |
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Back'</span>, sentences_path <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Now, we modify the index in order to retrieve the second operation: the &#8220;read&#8221;. Basically, clicking on the corresponding line, will read the text previously stored in the db.</p>
<p>We only need to add a line to obtain this view:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">#appviewssentenceindex.html.erb
&lt;h1&gt;Listing sentences&lt;/h1&gt;
&nbsp;
&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;Body&lt;/th&gt;
  &lt;/tr&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#0066ff; font-weight:bold;">@sentences</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>sentence<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;tr&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>=h sentence.<span style="color:#9900CC;">body</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Read'</span>, read_sentence_path<span style="color:#006600; font-weight:bold;">&#40;</span>sentence<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Show'</span>, sentence <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Edit'</span>, edit_sentence_path<span style="color:#006600; font-weight:bold;">&#40;</span>sentence<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Destroy'</span>, sentence, <span style="color:#ff3333; font-weight:bold;">:confirm</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Are you sure?'</span>, <span style="color:#ff3333; font-weight:bold;">:method</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:delete</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
  &lt;/tr&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&lt;/table&gt;
&nbsp;
&lt;br /&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'New sentence'</span>, new_sentence_path <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>We modify the routing:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#configroutes.rb</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#change map.resources :sentences with</span>
map.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:sentences</span>, <span style="color:#ff3333; font-weight:bold;">:member</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:read</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:get</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#ff3333; font-weight:bold;">:collection</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:speak</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:put</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#add</span>
map.<span style="color:#9900CC;">root</span> <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;sentences&quot;</span></pre></div></div>

<p>Configure the database.yml file, we create the db and tables with rake and start up Glassfish as explained in this <a href="http://mastrodonato.info/index.php/2009/08/setting-up-an-environment-for-jruby-on-rails/?lang=en">previous article</a> (find database.yml).</p>
<p>As we have seen, using java libraries with jruby on rails is very simple, we are ready to make our server to say the most dirty words!</p>
<p>The <strong>project</strong> can be downloaded from <a href="http://www.gigasize.com/get.php?d=p4wsyz5z29c">here</a>.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/08/freetts-a-java-library-used-from-jruby-on-rails/feed/?lang=en</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting up an environment for jRuby on Rails</title>
		<link>http://mastrodonato.info/index.php/2009/08/setting-up-an-environment-for-jruby-on-rails/?lang=en</link>
		<comments>http://mastrodonato.info/index.php/2009/08/setting-up-an-environment-for-jruby-on-rails/?lang=en#comments</comments>
		<pubDate>Thu, 20 Aug 2009 21:22:41 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby @en]]></category>
		<category><![CDATA[Glassfish]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[jRuby]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=207</guid>
		<description><![CDATA[My apologize in advance for having used a robot translator, a small help to save a little time: Over the next few articles i will use jruby 1.3.1 with Rails 2.3.3 and this is an opportunity to write two lines about how to configure the system. By the way, the system used is a virtualized [...]]]></description>
			<content:encoded><![CDATA[<p><strong>My apologize in advance for having used a robot translator, a small help to save a little time:</strong></p>
<p>Over the next few articles i will use jruby 1.3.1 with Rails 2.3.3 and this is an opportunity to write two lines about how to configure the system.<br />
By the way, the system used is a virtualized windows xp, java sdk 6, Glassfish as application server and javadb.</p>
<p>Download and install the <a href="http://java.sun.com/javase/downloads/index.jsp" target="_blank">java sdk</a>, at the moment we are at version 6 update 14.</p>
<p>There is also the sdk with NetBeans, an excellent IDE to manage projects ruby on rails. It could be an opportunity to try it. The package also includes the application server Glassfish V3.</p>
<p>Now we download and install <a href="http://www.jruby.org/" target="_blank">jRuby</a>, atm we are at version 1.3.1. </p>
<p>Installation is simple: unpack the zip in a path like C:rubyjruby-131</p>
<p>Let&#8217;s set the environment variables, right click on &#8220;My Computer&#8221; icon on the desktop -> Properties -> select the tab &#8220;Advanced&#8221; -> click on &#8220;environment variables&#8221;.</p>
<ul>
<li>Add to the <strong>PATH</strong> variable the path to the bin folder where we installed jruby, for example C:rubyjruby-131bin.</li>
<li>Set the environment variable <strong>JAVA_HOME</strong>. In the &#8220;system variables&#8221; -> &#8220;New&#8221; -> as the name &#8220;JAVA_HOME&#8221; value as the installation path dell&#8217;sdk for example C:ProgrammiJavajdk1.6.0_14</li>
</ul>
<p>It is not necessary to restart the operating system, we test the result:<br />
<code><br />
C:><strong>jruby -v</strong><br />
jruby 1.3.1 (ruby 1.8.6p287) (2009-06-15 2fd6c3d) (Java HotSpot(TM) Client VM 1.6.0_14) [x86-java]<br />
</code></p>
<p>Eureka! Now we&#8217;re ready to use jruby, jirb and gem. <strong>If in your system there is also classic ruby you may specify jruby-S </strong> to recall gem otherwise you can also put jruby path in the last position inside environment variable. Ok, now we can install rails 2.3.3:<br />
<code><br />
C:><strong>jruby -S gem install rails</strong><br />
Successfully installed activesupport-2.3.3<br />
Successfully installed activerecord-2.3.3<br />
Successfully installed actionpack-2.3.3<br />
Successfully installed actionmailer-2.3.3<br />
Successfully installed activeresource-2.3.3<br />
Successfully installed rails-2.3.3<br />
6 gems installed<br />
Installing ri documentation for activesupport-2.3.3...<br />
Installing ri documentation for activerecord-2.3.3...<br />
Installing ri documentation for actionpack-2.3.3...<br />
Installing ri documentation for actionmailer-2.3.3...<br />
Installing ri documentation for activeresource-2.3.3...<br />
Installing ri documentation for rails-2.3.3...<br />
Installing RDoc documentation for activesupport-2.3.3...<br />
Installing RDoc documentation for activerecord-2.3.3...<br />
Installing RDoc documentation for actionpack-2.3.3...<br />
Installing RDoc documentation for actionmailer-2.3.3...<br />
Installing RDoc documentation for activeresource-2.3.3...<br />
Installing RDoc documentation for rails-2.3.3...<br />
</code></p>
<p>We install the jdbc adapter to connect to Java DB (i don&#8217;t use jruby-S anymore, in my system is not necessary):<br />
<code><br />
C:><strong>gem install activerecord-jdbcderby-adapter</strong><br />
Successfully installed activerecord-jdbc-adapter-0.9.1<br />
Successfully installed jdbc-derby-10.4.2.0<br />
Successfully installed activerecord-jdbcderby-adapter-0.9.1<br />
3 gems installed<br />
Installing ri documentation for activerecord-jdbc-adapter-0.9.1...<br />
Installing ri documentation for jdbc-derby-10.4.2.0...<br />
Installing ri documentation for activerecord-jdbcderby-adapter-0.9.1...<br />
Installing RDoc documentation for activerecord-jdbc-adapter-0.9.1...<br />
Installing RDoc documentation for jdbc-derby-10.4.2.0...<br />
Installing RDoc documentation for activerecord-jdbcderby-adapter-0.9.1...<br />
</code></p>
<p>Then install the gem for Glassfish:<br />
<code><br />
C:><strong>gem install glassfish</strong><br />
Successfully installed rack-1.0.0<br />
Successfully installed glassfish-0.9.5-universal-java<br />
2 gems installed<br />
Installing ri documentation for rack-1.0.0...<br />
Installing ri documentation for glassfish-0.9.5-universal-java...<br />
Installing RDoc documentation for rack-1.0.0...<br />
Installing RDoc documentation for glassfish-0.9.5-universal-java...<br />
</code></p>
<p>We are ready, we create a new project:<br />
<code><br />
C:>rails ProvaArticolo<br />
</code></p>
<p>&#8230; in the database&#8217;s configuration file we use:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># config/database.yml</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># JavaDB Setup</span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#008000; font-style:italic;"># You may need to copy derby.jar into</span>
<span style="color:#008000; font-style:italic;">#  TODO: location C:rubyjruby-131lib</span>
<span style="color:#008000; font-style:italic;"># With Java SE 6 and later this is not necessary.</span>
development:
  adapter: derby
  database: db<span style="color:#006600; font-weight:bold;">/</span>development.<span style="color:#9900CC;">db</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Warning: The database defined as 'test' will be erased and</span>
<span style="color:#008000; font-style:italic;"># re-generated from your development database when you run 'rake'.</span>
<span style="color:#008000; font-style:italic;"># Do not set this db to the same as development or production.</span>
test:
  adapter: derby
  database: db<span style="color:#006600; font-weight:bold;">/</span>test.<span style="color:#9900CC;">db</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Warning: The database defined as 'test' will be erased and</span>
<span style="color:#008000; font-style:italic;"># re-generated from your development database when you run 'rake'.</span>
<span style="color:#008000; font-style:italic;"># Do not set this db to the same as development or production.</span>
production:
  adapter: derby
  database: db<span style="color:#006600; font-weight:bold;">/</span>production.<span style="color:#9900CC;">db</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Warning: The database defined as 'test' will be erased and</span>
<span style="color:#008000; font-style:italic;"># re-generated from your development database when you run 'rake'.</span>
<span style="color:#008000; font-style:italic;"># Do not set this db to the same as development or production.</span></pre></div></div>

<p>Let us start the server, from the prompt inside our application&#8217;s directory:<br />
<code><br />
C:ProvaArticolo><strong>glassfish</strong><br />
Starting GlassFish server at: 127.0.0.1:3000 in development environment...<br />
Writing log messages to: C:/ProvaArticolo/log/development.log.<br />
Press Ctrl+C to stop.<br />
</code></p>
<p>At http://localhost:3000/ should see the welcome page.<br />
Now stop the server with Ctrl + C db and create the environment in development: </p>
<p><code>C:ProvaArticolo><strong>rake db:create</strong><br />
(in C:/ProvaArticolo)<br />
db/development.db already exists</code></p>
<p>We create something within the database, we start with the source of rails:<br />
<code><br />
C:ProvaArticolo>jruby script/generate scaffold article name:string body:text<br />
      create  app/models/<br />
      exists  app/controllers/<br />
      exists  app/helpers/<br />
      exists  app/views/articles<br />
      create  app/views/layouts/<br />
      create  test/functional/<br />
      create  test/unit/<br />
      create  test/unit/helpers/<br />
      create  public/stylesheets/<br />
      create  app/views/articles/index.html.erb<br />
      create  app/views/articles/show.html.erb<br />
      create  app/views/articles/new.html.erb<br />
      create  app/views/articles/edit.html.erb<br />
      create  app/views/layouts/articles.html.erb<br />
      create  public/stylesheets/scaffold.css<br />
      create  app/controllers/articles_controller.rb<br />
      create  test/functional/articles_controller_test.rb<br />
      create  app/helpers/articles_helper.rb<br />
      create  test/unit/helpers/articles_helper_test.rb<br />
       route  map.resources :articles<br />
  dependency  model<br />
      exists    app/models/<br />
      exists    test/unit/<br />
      create    test/fixtures/<br />
      create    app/models/article.rb<br />
      create    test/unit/article_test.rb<br />
      create    test/fixtures/articles.yml<br />
      exists    db/migrate<br />
      create    db/migrate/20090729171105_create_articles.rb<br />
</code></p>
<p>and create the table:<br />
<code><br />
<strong>C:ProvaArticolo>rake db:migrate</strong><br />
(in C:/ProvaArticolo)<br />
==  CreateArticles: migrating =================================================<br />
-- create_table(:articles)<br />
   -> 0.0700s<br />
   -> 0 rows<br />
==  CreateArticles: migrated (0.0700s) ========================================<br />
</code></p>
<p>Finally remove the index.html file from inside the public folder and create the initial route:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#configroutes.rb</span>
map.<span style="color:#9900CC;">root</span> <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;articles&quot;</span></pre></div></div>

<p>Reboot the server and at http://localhost:3000/ this time we should see a list of articles.</p>
<p>If I don&#8217;t have forgotten something, jruby on rails is ready, enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/08/setting-up-an-environment-for-jruby-on-rails/feed/?lang=en</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
