<?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/" rel="self" type="application/rss+xml" />
	<link>http://mastrodonato.info</link>
	<description>Non c&#039;e&#039; prezzo per la miticita&#039;</description>
	<lastBuildDate>Thu, 01 Apr 2010 12:58:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creare scheletri di applicazioni con generatori e template</title>
		<link>http://mastrodonato.info/index.php/2010/03/creare-scheletri-di-applicazioni-con-generatori-e-modelli/</link>
		<comments>http://mastrodonato.info/index.php/2010/03/creare-scheletri-di-applicazioni-con-generatori-e-modelli/#comments</comments>
		<pubDate>Sat, 20 Mar 2010 20:32:54 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Generatori]]></category>
		<category><![CDATA[nifty-generators]]></category>
		<category><![CDATA[Templates]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=481</guid>
		<description><![CDATA[Ultima modifica 01/04/2010
Rails è un meraviglioso strumento che permette di realizzare agevolmente, un&#8217;applicazione secondo gli standard moderni. Tuttavia, quando se ne crea una nuova, è necessario eseguire diverse operazioni alquanto noiose: aggiungere plugins o gemme, il recupero di un layout di partenza e tutto ciò che di solito usate nelle vostre applicazioni.
Per risolvere questo problema, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Ultima modifica 01/04/2010</strong></p>
<p>Rails è un meraviglioso strumento che permette di realizzare agevolmente, un&#8217;applicazione secondo gli standard moderni. Tuttavia, quando se ne crea una nuova, è necessario eseguire diverse operazioni alquanto noiose: aggiungere plugins o gemme, il recupero di un layout di partenza e tutto ciò che di solito usate nelle vostre applicazioni.<br />
Per risolvere questo problema, dalla versione 2.3 è possibile utilizzare i templates, modelli con cui è possibile diversificare la creazione di scheletri di applicazioni. Si trovano molti esempi su internet, io ne presento uno che mostra i vantaggi in combinazione con un generatore personalizzato per le proprie esigenze. <a href="http://railscasts.com/episodes/58-how-to-make-a-generator">Questa guida </a> spiega come creare un generatore. Se siete impazienti potete partire da qualcosa di già fatto come ho fatto io. Ho personalizzato il nifty-generators di Ryan Bates aggiungendo alcune nuove caratteristiche:</p>
<ul>
<li>Layout: ora supporta stili multipli: classic, cloudy and blackwhite. Niente di che, solo una base da sviluppare</li>
<li>Layout: ho inserito il file di configurazione config.rb, contenitore delle costanti applicative come il nome dell&#8217;applicazione, lo stile di default ecc.</li>
<li>Ho introdotto la localizzazione: il nifty_layout aggiunge i files yaml per la lingua en e it. Nell&#8217;application controller viene inserito del codice per la gestione, con il parametro lang si cambia l&#8217;impostazione della lingua (esempio ?lang=it) che verrà mantenuta per tutta la sessione. Il nifty_scaffold aggiunge le risorse e i suoi attributi, pronti per essere tradotti.</li>
<li>nifty_layout: la div per i messaggi è stata spostata dentro un partial nella cartella shared. Ho preferito fare questo per poterla utilizzare anche tramite ajax</li>
<li>nifty_scaffold: le views new e edit, ora utilizzano due partial: render @model che a sua volta richiama il secondo con all&#8217;interno i campi. Come la versione originale, supporta haml e sass ed ho aggiunto il supporto a formtastic potendo quindi scegliere la combinazione preferita!</li>
</ul>
<p></p>
<p>La nuova versione del nifty-generators è scaricata dal template che vi mostrerò, comunque, questi sono i <a href="http://github.com/marcomd/nifty-generators">sorgenti</a>, <a href="http://github.com/marcomd/nifty-generators/downloads">qua</a> invece, potete scaricare l&#8217;ultima versione della gemma e del template.</p>
<p>Cos&#8217;è che dovrebbe fare il generatore e cosa il template?<br />
Beh, i templates dovrebbero essere un contenitore di operazioni da compiere, eseguiti nella fase di creazione di un nuovo progetto. Con i generatori invece, è possibile fare quasi tutto. In genere, si aggiungono nuove funzionalità o si personalizza qualcosa che già esiste. Può essere eseguito ogni volta che si vuole da riga di comando e naturalmente, richiamato all&#8217;interno di un template.</p>
<p>Chi non ha mai usato un template, potrebbe iniziare con un semplice esempio o meglio, con un tutorial:<br />
questo <a href="http://railscasts.com/episodes/148-app-templates-in-rails-2-3">screencast</a> mostra come destreggiarsi e questo invece, è <a href="http://m.onkey.org/2008/12/4/rails-templates">un articolo</a> in html.</p>
<p>I molti esempi che ho visto su internet sono spesso pieni di parametri di configurazione che mi fan venire dei grossi mal di testa. Io invece ho preferito che mi venisse chiesto cosa voglio. Sicuramente, non è ancora perfetto, ma penso che sia una buona 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>Questo è uno dei possibili risultati:</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>Ecco fatto, ora avviamo mongrel</p>
<p><code><br />
marco@d9400:~/Rails$ cd Blog/<br />
marco@d9400:~/Rails/Blog$ script/server</p>
<p>=> 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>http://localhost:3000 e controlliamo il nostro nuovo blog</p>
<p><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="Questo è solo l'inizio" width="300" height="107" class="aligncenter size-medium wp-image-462" /></a></p>
<p>Ora aggiungiamo una risorsa e lanciamo la migrate per allineare il db:</p>
<p><code><br />
marco@d9400:~/Rails$ cd Blog/<br />
marco@d9400:~/Rails/Blog$ script/generate nifty_scaffold post title:string body:text</p>
<p>      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</p>
<p>marco@d9400:~/Rails/Blog$ rake db:migrate</p>
<p>(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>I controllers e le viste usano messaggi localizzati, per questo motivo i nomi delle risorse devono essere aggiunti all&#8217;interno degli yaml. Dalla versione 0.3.2.3 nifty_scafold lo fa per voi, dovete pensare solo alla traduzione. Tenete presente inoltre, che se per esempio distruggete una risorsa deve essere rimossa dagli yaml manualmente.</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>Ho usato dei commenti del tipo: <em>#DO NOT REMOVE ecc.</em> per posizionare i nomi delle risorse o degli attributi nelle posizioni corrette, per cui è necessario non rimuoverli.</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="Messaggio creato con successo" title="Messaggio creato con successo" width="300" height="156" class="aligncenter size-medium wp-image-469" /></a></p>
<p>Ho da poco iniziato a testare la versione haml e sass per cui potrebbero esserci ancora dei bachi.<br />
Spero che questo articolo possa essere stato utile, buon divertimento!</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2010/03/creare-scheletri-di-applicazioni-con-generatori-e-modelli/feed/</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/</link>
		<comments>http://mastrodonato.info/index.php/2010/03/ruby-vs-python-vs-windows-vs-linux/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 22:33:00 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[IronRuby .NET]]></category>
		<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Benchmark]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=432</guid>
		<description><![CDATA[Nel precedente articolo, ho esaminato le prestazioni di alcuni interpreti ruby e python, su un sistema windows xp. Questa volta, ho eseguito gli stessi script sotto linux: distribuzione ubuntu 9.10 2.6.31-20-generic. Le versioni APT sono state installate tramite gestore di pacchetti, le RVM invece, tramite il Ruby Version Manager e compilate dai sorgenti.


Versione
Compilatore/Sistema/VM
Secondi


JRuby 1.4.0 APT
 [...]]]></description>
			<content:encoded><![CDATA[<p>Nel <a href="http://mastrodonato.info/index.php/2010/01/ruby-vs-ruby-vs-python-vs-python/">precedente articolo</a>, ho esaminato le prestazioni di alcuni interpreti ruby e python, su un sistema windows xp. Questa volta, ho eseguito gli stessi script sotto linux: distribuzione ubuntu 9.10 2.6.31-20-generic. Le versioni APT sono state installate tramite gestore di pacchetti, le RVM invece, tramite il <a href="http://rvm.beginrescueend.com/">Ruby Version Manager</a> e compilate dai sorgenti.</p>
<p><center></p>
<table>
<th>Versione</th>
<th>Compilatore/Sistema/VM</th>
<th>Secondi</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/feed/</wfw:commentRss>
		<slash:comments>4</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/</link>
		<comments>http://mastrodonato.info/index.php/2010/01/ruby-vs-ruby-vs-python-vs-python/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 22:23:55 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[IronRuby .NET]]></category>
		<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Benchmark]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=388</guid>
		<description><![CDATA[Un altro benchmark che mette a confronto alcune recenti versioni di ruby con gli ultimi rilasci di python. Due semplici script per confrontare sintassi e prestazioni di questi moderni linguaggi.
Il sistema su cui ho eseguito la prova è un portatile Dell Inspiron 9400 con Centrino Duo, Intel T7200 4Mb Cache 2Ghz, Ram 2Gb 667Mhz. Sistema [...]]]></description>
			<content:encoded><![CDATA[<p>Un altro benchmark che mette a confronto alcune recenti versioni di ruby con gli ultimi rilasci di python. Due semplici script per confrontare sintassi e prestazioni di questi moderni linguaggi.<br />
Il sistema su cui ho eseguito la prova è un portatile Dell Inspiron 9400 con Centrino Duo, Intel T7200 4Mb Cache 2Ghz, Ram 2Gb 667Mhz. Sistema operativo Windows XP pro SP3.</p>
<p>Questo è il risultato del primo test, usato come riscaldamento per le vm:</p>
<p><center></p>
<table>
<th>Versione</th>
<th>Compilatore</th>
<th>Secondi</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 senza warm up" title="Bench MM1" width="300" height="121" class="size-medium wp-image-397" /></a><p class="wp-caption-text">Benchmark senza warm up</p></div>
<p>Il risultato che segue invece, si riferisce alla media delle tre rilevazioni successive al riscaldamento. Le prestazioni di JRuby migliorano del 23%:</p>
<p><center></p>
<table>
<th>Versione</th>
<th>Compilatore</th>
<th>Secondi</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="Rilevazioni dopo warm up" title="BenchMM1_warmup" width="300" height="121" class="size-medium wp-image-408" /></a><p class="wp-caption-text">Rilevazioni dopo warm up</p></div>
<p>Ed ecco gli script. Ho cercato di ottimizzare le rispettive versioni e per fare ciò, ho dovuto creare due varianti per ogni linguaggio. </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/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Gestire instradamenti errati</title>
		<link>http://mastrodonato.info/index.php/2009/11/gestire-instradamenti-errati/</link>
		<comments>http://mastrodonato.info/index.php/2009/11/gestire-instradamenti-errati/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 11:28:25 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Instradamenti]]></category>
		<category><![CDATA[Routes]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=89</guid>
		<description><![CDATA[La nostra applicazione può rispondere solo a determinate richieste, naturalmente a quelle che abbiamo previsto. Per tutte le altre viene generato un errore in base al tipo:

Errori di instradamento
Errori nel controller

Errori di instradamento
Piccola premessa: In un&#8217;applicazione tradizionale, equivaleva a richiamare una pagina inesistente ricevendo dal web server un codice 404. In un framework con un [...]]]></description>
			<content:encoded><![CDATA[<p>La nostra applicazione può rispondere solo a determinate richieste, naturalmente a quelle che abbiamo previsto. Per tutte le altre viene generato un errore in base al tipo:</p>
<ul>
<li>Errori di instradamento</li>
<li>Errori nel controller</li>
</ul>
<h3>Errori di instradamento</h3>
<p><strong>Piccola premessa</strong>: In un&#8217;applicazione tradizionale, equivaleva a richiamare una pagina inesistente ricevendo dal web server un codice 404. In un framework con un pattern mvc, le richieste transitano attraverso un controller e questo ci permette di definirne il comportamento. Le richieste vengono smistate in base alle regole di instradamento ed in Rails, per convenzione, tutti i controllers possono essere indirizzati, in fondo al file routes.rb scopriamo il perchè:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#config\routes.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>Per fare in modo che solo determinati controllers possano essere instradati, basta eliminare quelle due righe in fondo al file che definiscono solo la forma della richiesta, lasciando solo gli instradamenti espliciti. Fine della premessa.</p>
<p>Se non esiste una rotta, viene generato un <strong>errore di instradamento</strong> (routing error) poichè Rails non sa cosa intendiamo richiamare. Per gestire questa evenienza, basta semplicemente aggiungere un&#8217;ultima strada prima di generare l&#8217;errore che percorrerà solo per informare che non ha trovato altre strade valide:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#config\routes.rb</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#aggiungiamo in ultima riga, in coda a tutto</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></td></tr></table></div>

<p>Quest&#8217;ultima rotta cattura gli instradamenti non gestiti e li indirizza nel controller dove vogliamo gestirli, nell&#8217;esempio indirizza nel controller home col metodo unknown_request:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#app\controllers\home_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>Errori nel controller</h3>
<p>Se il controller è stato indirizzato non è detto che la richiesta sia comunque corretta, ognuno infatti ha una serie di operazioni che devono essere previste e devono poter operare sulla risorsa quindi gestire le eccezioni sollevate dal database.</p>
<p>Dalla versione 2.0 di rails, possiamo utilizzare rescue_from nel controller:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#app\controllers\my_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>Queste sono le viste:</p>

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

<p>Usare una proc come parametro a rescue_from potrebbe essere un&#8217;alternativa più concisa:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#app\controllers\my_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/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comparazione linguaggi script per la geometria frattale</title>
		<link>http://mastrodonato.info/index.php/2009/08/comparazione-linguaggi-script-per-la-geometria-frattale/</link>
		<comments>http://mastrodonato.info/index.php/2009/08/comparazione-linguaggi-script-per-la-geometria-frattale/#comments</comments>
		<pubDate>Sat, 22 Aug 2009 23:38:24 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[IronRuby .NET]]></category>
		<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Ruby]]></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>

		<guid isPermaLink="false">http://mastrodonato.info/?p=209</guid>
		<description><![CDATA[In questo articolo metterò a confronto le ultime incarnazioni di ruby, con le ultime di python, groovy, php, lua, perl e anche java, per avere un metro di paragone con un linguaggio precompilato. Vedremo, infatti, come si comportano i linguaggi di script applicati alla geometria frattale, più precisamente un algoritmo della famiglia di Mandelbrot.
Navigando, ho [...]]]></description>
			<content:encoded><![CDATA[<p>In questo articolo metterò a confronto le ultime incarnazioni di ruby, con le ultime di python, groovy, php, lua, perl e anche java, per avere un metro di paragone con un linguaggio precompilato. Vedremo, infatti, come si comportano i linguaggi di script applicati alla geometria frattale, più precisamente un algoritmo della famiglia di Mandelbrot.<br />
Navigando, ho trovato un <a href="http://www.timestretch.com/FractalBenchmark.html#4d9e0f15fc1b42420cf3f778c3cccad7">confronto</a> molto interessante ma un pò datato, risale a più di due anni fa. Da allora sono cambiate un pò di cose e ne ho approfittato per fare un aggiornamento anche se non includerò tutti quei linguaggi molti dei quali semi sconosciuti. Questa è l&#8217;occasione per mettere a confronto ruby e python anche nelle loro versioni java e .net, un intenzione che avevo già da un pò di tempo.</p>
<p>Usare un frattale come benchmark è oltretutto comodo: se un tentativo di ottimizzazione non va a buon fine se ne ha subito l&#8217;evidenza ed essendo disegnato in tempo reale, si riesce a percepire la velocità di esecuzione. Il frattale è disegnato in ascii anche perchè l&#8217;utilizzo di librerie esterne ne avrebbe drogato l&#8217;esito.</p>
<pre>
                                       *
                                       *
                                       *
                                       *
                                       *
                                      ***
                                     *****
                                     *****
                                      ***
                                       *
                                   *********
                                 *************
                                ***************
                             *********************
                             *********************
                              *******************
                              *******************
                              *******************
                              *******************
                            ***********************
                              *******************
                              *******************
                             *********************
                              *******************
                              *******************
                               *****************
                                ***************
                                 *************
                                   *********
                                       *
                                ***************
                            ***********************
                         * ************************* *
                         *****************************
                      * ******************************* *
                       *********************************
                      ***********************************
                    ***************************************
               *** ***************************************** ***
               *************************************************
                ***********************************************
                 *********************************************
                 *********************************************
                ***********************************************
                ***********************************************
              ***************************************************
               *************************************************
               *************************************************
              ***************************************************
              ***************************************************
         *    ***************************************************    *
       *****  ***************************************************  *****
       ****** *************************************************** ******
      ******* *************************************************** *******
    ***********************************************************************
    ********* *************************************************** *********
       ****** *************************************************** ******
       *****  ***************************************************  *****
              ***************************************************
              ***************************************************
              ***************************************************
              ***************************************************
               *************************************************
               *************************************************
              ***************************************************
                ***********************************************
                ***********************************************
                  *******************************************
                   *****************************************
                 *********************************************
                **** ****************** ****************** ****
                 ***  ****************   ****************  ***
                  *    **************     **************    *
                         ***********       ***********
                         **  *****           *****  **
                          *   *                 *   *
</pre>
<p>Questi sono i dati del sistema:<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>Questi sono i risultati prestazionali ottenuti da una media di cinque rilevazioni catturate dopo qualche tentativo (non mi sono fidato dello startup delle VM):</p>
<pre>
Linguaggio      Tempo (in secondi)  n più lento di 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>Questo è il grafico, naturalmente <strong>valori più bassi indicano una prestazione migliore</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>Questi sono gli script usati per generare il frattale, andavano benissimo quelli di Erik Wrenholt, mi sono limitato a qualche semplice modifica per far funzionare python 3.1 o per migliorare lievemente la già ottima leggibilità in ruby e 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
</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;<span style="color: #000099; font-weight: bold;">\n</span>&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>
			<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;<span style="color: #000099; font-weight: bold;">\n</span>Java 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;<span style="color: #000099; font-weight: bold;">\n</span>&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;<span style="color: #000099; font-weight: bold;">\n</span>&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;<span style="color: #000099; font-weight: bold;">\n</span>PHP 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;">'<span style="color: #000099; font-weight: bold;">\n</span>Python 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;<span style="color: #000099; font-weight: bold;">\n</span>Groovy 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;<span style="color:#000099;">\n</span>&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;<span style="color:#000099;">\n</span>Ruby 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;<span style="color: #000099; font-weight: bold;">\n</span>&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;<span style="color: #000099; font-weight: bold;">\n</span>&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>Commenti:</h3>
<p>La velocità di Lua è ormai nota, solamente 5 volte più lento rispetto al codice java compilato, il miglior risultato. La sua semplicità è la sua forza, forse è proprio questo che lo rende così veloce? E&#8217; stato adottato dalla Blizzard all&#8217;interno del gioco World of Warcraft e se l&#8217;hanno fatto loro un motivo ci sarà. Non è ad oggetti o meglio, nativamente non li supporta anche se c&#8217;è il progetto <a href="http://loop.luaforge.net/">LOOP</a> che lo estende a questo modello di programmazione.<br />
Php e Perl non hanno bisogno di commenti.<br />
Tra le versioni C di Ruby e Python è chiaramente avanti quest&#8217;ultimo. Il paragone equo sarebbe la Rb1.8.6 con la Py2.6.2 e la Rb1.9.1 con la Py3.1.1.<br />
La sfida tra le versioni che sfruttalo la Java VM: Groovy, Jython e JRuby, vede in vantaggio quest&#8217;ultimo. Groovy è molto indietro come prestazioni ma il dubbio più grosso che ho è: ma a chi è destinato? Come sintassi non è male ma ruby è ancora più scorrevole e poi ha quel rake che è tanto comodo per tante cose.<br />
Le versioni ruby e python per .Net vede incredibilmente avanti IronPython! Ma che gli hanno messo dentro, la dinamite? Sarà molto interessante esaminare il nuovo framework MVC di ASP.NET da poco arrivato alla versione 1 e che sarà incluso nel framework .Net 4, esistono progetti sia per IronRuby che per IronPython.<br />
Se questo articolo è risultato interessante, forse potrai trovare qualcos&#8217;altro tra gli annunci del mio sponsor, si trova nella colonna a destra, grazie!</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/08/comparazione-linguaggi-script-per-la-geometria-frattale/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Cinque rubini nella sfida multicore</title>
		<link>http://mastrodonato.info/index.php/2009/08/cinque-rubini-nella-sfida-multicore/</link>
		<comments>http://mastrodonato.info/index.php/2009/08/cinque-rubini-nella-sfida-multicore/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 15:48:10 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[IronRuby .NET]]></category>
		<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Benchmark]]></category>
		<category><![CDATA[multicore]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=133</guid>
		<description><![CDATA[Il sistema su cui ho eseguito la prova è un portatile: Dell Inspiron 9400 con Centrino Duo, Intel T7200 4Mb Cache 2Ghz (166&#215;12) Ram 2Gb 667Mhz. Si tratta di un sistema fisico Windows XP pro SP3.
Lo scopo: verificare lo sfruttamento di più core del processore (due nel mio caso) mettendo a confronto un processo singolo [...]]]></description>
			<content:encoded><![CDATA[<p>Il sistema su cui ho eseguito la prova è un portatile: Dell Inspiron 9400 con Centrino Duo, Intel T7200 4Mb Cache 2Ghz (166&#215;12) Ram 2Gb 667Mhz. Si tratta di un sistema fisico Windows XP pro SP3.</p>
<p>Lo <strong>scopo</strong>: verificare lo sfruttamento di più core del processore (due nel mio caso) mettendo a confronto un processo singolo con una doppia esecuzione in contemporanea.</p>
<p>Per fare questo ho utilizzato il bench delle stringhe di un <a href="http://mastrodonato.info/index.php/2009/08/quattro-interpreti-ruby-a-confronto/">articolo precedente</a>.<br />
C&#8217;è da tener presente che nell&#8217;esecuzione del doppio processo, ognuno deve condividere la cpu con il sistema operativo (il carico è sempre al 100%) mentre nel test singolo questo non succede, un core è dedicato al test mentre l&#8217;altro alle altre faccende. Per questo motivo il sistema operativo era privo di antivirus e di altri processi/servizi pesanti.</p>
<p>Cominciamo con il vecchio interprete dell&#8217;oneclick installer:</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>
Singolo:</p>
<pre>
--- Total:            80.953000   9.531000  90.484000 ( 90.906000)
</pre>
<p>Multicore:</p>
<pre>
CORE1:
--- Total:            87.063000  19.531000 106.594000 (107.923000)

CORE2:
--- Total:            91.344000  18.375000 109.719000 (110.125000)
</pre>
<p>Questo è il decremento del test doppio processo rispetto l'esecuzione singola:<br />
Decremento del 10%<br />
Decremento reale del 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>
Singolo:</p>
<pre>
--- Total:            71.641000   8.203000  79.844000 ( 80.093750)
</pre>
<p>Multicore:</p>
<pre>
CORE1:
--- Total:            80.797000  17.750000  98.547000 ( 99.093750)
CORE2:
--- Total:            76.500000  19.375000  95.875000 ( 97.187500)
</pre>
<p>Decremento del 9,8%<br />
Decremento reale del 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>
Singolo:</p>
<pre>
--- Total:            48.594000   9.172000  57.766000 ( 58.046875)
</pre>
<p>Multicore:</p>
<pre>
CORE1:
--- Total:            56.345000  19.219000  75.564000 ( 76.312500)
CORE2:
--- Total:            52.109000  20.703000  72.812000 ( 74.156250)
</pre>
<p>Decremento del 11,5%<br />
Decremento reale del 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>
Singolo:</p>
<pre>
--- Total:            54.686000   0.000000  54.686000 ( 54.655000)
</pre>
<p>Multicore:</p>
<pre>
CORE1:
--- Total:            65.170000   0.000000  65.170000 ( 64.920000)
CORE2:
--- Total:            64.234000   0.000000  64.234000 ( 64.000000)
</pre>
<p>Decremento del 17,2%<br />
Decremento reale del 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>
Singolo:</p>
<pre>
--- Total:           247.062500  34.328125 281.390625 (282.078125)
</pre>
<p>Multicore:</p>
<pre>
CORE1:
--- Total:           315.531250  40.578125 356.109375 (359.500000)
CORE2:
--- Total:           300.484375  46.156250 346.640625 (350.265625)
</pre>
<p>Decremento del 24,5%<br />
Decremento reale del 25,8%</p>
<h3>Riepilogo</h3>
<p><strong>Ruby 1.8.6 patch 111</strong><br />
Decremento del 10%<br />
Decremento reale del 20%</p>
<p><strong>Ruby 1.8.6 patch 368</strong><br />
Decremento del 9,8%<br />
Decremento reale del 22,5%</p>
<p><strong>Ruby 1.9.1</strong><br />
Decremento del 11,5%<br />
Decremento reale del 29,6%</p>
<p><strong>jRuby 1.3.1</strong><br />
Decremento del 17,2%<br />
Decremento reale del 17,9%</p>
<p><strong>IronRuby 0.9.0</strong><br />
Decremento del 24,5%<br />
Decremento reale del 25,8%</p>
<h3>Conclusioni</h3>
<p>Non so cosa sarebe successo con altri test ma in questo, <strong>jRuby</strong> è l'unico che ha un carico uniforme sui due core e lo fa costantemente quasi per tutta la durata. Solo alla fine si sposta verso un core ma con più discrezione. Ottima anche la prestazione con i due processi in contemporanea. </p>
<p>Le versioni 1.8.6 e 1.9.1 sono abbastanza allineate e riescono a spartire il carico solo sui test "Add". La prestazione del doppio processo non sarebbe male ma crea qualche difficoltà al sistema (almeno al mio) che ne allunga i tempi.</p>
<p>IronRuby ha qualche problemino con le stringhe già evidenziato in un <a href="http://mastrodonato.info/index.php/2009/08/quattro-interpreti-ruby-a-confronto/">articolo precedente</a>. Ignorando questo, non ha sfruttato il secondo core che è rimasto scarico per tutta la durata. Col secondo core libero, mi sarei aspettato un decremento meno evidente nel test del doppio processo ma così non è stato, questo è strano!</p>
<p>In un prossimo articolo, mi piacerebbe ripetere il test su un server quad core.</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/08/cinque-rubini-nella-sfida-multicore/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quattro interpreti ruby a confronto</title>
		<link>http://mastrodonato.info/index.php/2009/08/quattro-interpreti-ruby-a-confronto/</link>
		<comments>http://mastrodonato.info/index.php/2009/08/quattro-interpreti-ruby-a-confronto/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 21:59:56 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[IronRuby .NET]]></category>
		<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Benchmark]]></category>
		<category><![CDATA[IronRuby]]></category>
		<category><![CDATA[jRuby]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=103</guid>
		<description><![CDATA[Non si può negare che la versatilità sia una delle sue caratteristiche; un vero multipiattaforma. Ci sono interpreti per molti sistemi operativi e oltre alla versione classica in C++ abbiamo la valida alternativa di JRuby per il mondo java e infine Ironruby per il framework .NET di Microsoft, arrivato in questi ultimi giorni ad una [...]]]></description>
			<content:encoded><![CDATA[<p>Non si può negare che la versatilità sia una delle sue caratteristiche; un vero multipiattaforma. Ci sono interpreti per molti sistemi operativi e oltre alla versione classica in C++ abbiamo la valida alternativa di JRuby per il mondo java e infine Ironruby per il framework .NET di Microsoft, arrivato in questi ultimi giorni ad una versione abbastanza matura.<br />
Niente male, pensiamo ai benefici che un&#8217;azienda potrebbe avere: un linguaggio semplice da imparare ma molto potente, ha un ottimo framework (uno su tutti, Rails) per sviluppare rapidamente applicazioni disponibili per esempio nella intranet e in aggiunta a tutto questo, la possibilità di usare le librerie java o .net.</p>
<p>In questo articolo vedremo come si comportano questi quattro interpreti:</p>
<ol>
<li>Ruby 1.8.6 patch 368 aggiornato al 2009-03-31</li>
<li>Ruby 1.9.1 patch 129 aggiornato al 2009-05-12 revisione 23412</li>
<li>jRuby 1.3.1 (ruby 1.8.6p287) 2009-06-15 Client VM 1.6.0 update 14</li>
<li>IronRuby 0.9.0.0 on .NET 2.0.0.0</li>
</ol>
<p>Lo <strong>scopo</strong>: per scoprire le differenze prestazionali principalmente nell&#8217;ambito del singolo interprete. Ci sono metodi e tecniche che portano allo stesso risultato e senza simili test difficilmente si può stabilire qual&#8217;è la pratica migliore.<br />
Nei test che vedremo, mi sono concentrato sulla velocità di elaborazione controllando solo grossolanamente la quantità di memoria utilizzata.</p>
<p>Il <strong>sistema</strong>: La macchina fisica è un Windows XP Professional SP3 32bit con un Intel E7300 dual core con 3Mb di cache e 3,25Gb di ram. </p>
<div id="attachment_111" class="wp-caption aligncenter" style="width: 429px"><img src="http://mastrodonato.info/wp-content/uploads/2009/08/FXP1.PNG" alt="Sistema fisico" title="FXP" width="419" height="534" class="size-full wp-image-111" /><p class="wp-caption-text">Sistema fisico</p></div>
<p>I test giravano su un sistema virtualizzato con MS Virtual PC 2007, os sempre XP Pro, Java SDK 6.14, Framework .NET 3.5 SP1, processore singolo core, memoria 768Mb. Solo il test più pesante sugli hash l&#8217;ho ripetuto con 2Gb.</p>
<p>I <strong>benchmarks</strong> sono divisi in quattro categorie:</p>
<ul>
<li>Stringhe</li>
<li>Numeri</li>
<li>Array</li>
<li>Hash</li>
</ul>
<p>Li ho creati io e si limitano ad eseguire operazioni molto semplici. Alla fine c&#8217;è un totale ma non deve essere inteso come un indice sull&#8217;effettiva prestazione dell&#8217;interprete perchè questa cambia molto in base ai test scelti e quelli che ho utilizzati io non è detto che siano condizioni riscontrate nell&#8217;uso reale.</p>
<h3>Le stringhe</h3>
<p>Questo è il primo script:</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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;benchmark&quot;</span>
<span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#CC00FF; font-weight:bold;">Benchmark</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">Benchmark</span>.<span style="color:#9900CC;">bm</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">21</span>, <span style="color:#996600;">&quot;--- Total:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><span style="color:#006600; font-weight:bold;">|</span>b<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Concat 1.000.000:&quot;</span>
  n=<span style="color:#006666;">1</span>_000_000
  t1 = b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;+&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;a &quot;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#996600;">&quot;b &quot;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#996600;">&quot;c &quot;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#996600;">&quot;d &quot;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#996600;">&quot;e &quot;</span>; <span style="color:#996600;">&quot;f &quot;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#996600;">&quot;g&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;&lt;&lt;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;a &quot;</span><span style="color:#006600; font-weight:bold;">&lt;&lt;</span><span style="color:#996600;">&quot;b &quot;</span><span style="color:#006600; font-weight:bold;">&lt;&lt;</span><span style="color:#996600;">&quot;c &quot;</span><span style="color:#006600; font-weight:bold;">&lt;&lt;</span><span style="color:#996600;">&quot;d &quot;</span><span style="color:#006600; font-weight:bold;">&lt;&lt;</span><span style="color:#996600;">&quot;e &quot;</span>; <span style="color:#996600;">&quot;f &quot;</span><span style="color:#006600; font-weight:bold;">&lt;&lt;</span><span style="color:#996600;">&quot;g&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    sa=<span style="color:#996600;">&quot;a&quot;</span>; sb=<span style="color:#996600;">&quot;b&quot;</span>; sc=<span style="color:#996600;">&quot;c&quot;</span>; sd=<span style="color:#996600;">&quot;d&quot;</span>; se=<span style="color:#996600;">&quot;e&quot;</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;#{sa} #{sb} #{sc} #{sd} #{se}&quot;</span>; <span style="color:#996600;">&quot;#{sb} #{sc}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Add 100.000:&quot;</span>
  n=<span style="color:#006666;">100</span>_000
  t2 = b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;+=&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#996600;">&quot;&quot;</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#996600;">&quot;.&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t2 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;&lt;&lt;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#996600;">&quot;&quot;</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;.&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t2 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;a = a + '.'&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#996600;">&quot;&quot;</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a = a <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;.&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t2 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#996600;">&quot;&quot;</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a = <span style="color:#996600;">&quot;#{a}.&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Other 100.000:&quot;</span>
  n=<span style="color:#006666;">100</span>_000
  str=<span style="color:#996600;">&quot;&quot;</span>
  t3 = b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;* 100:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> str = <span style="color:#996600;">&quot; abc def ghi rn&quot;</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">100</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;capitalize:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> str.<span style="color:#9900CC;">capitalize</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;upcase:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> str.<span style="color:#9900CC;">upcase</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;chomp:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> str.<span style="color:#CC0066; font-weight:bold;">chomp</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;include:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> str.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;ghi&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;str.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;qwertyuiopasdfghjkl&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;index:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> str.<span style="color:#9900CC;">index</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;ghi&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;str.<span style="color:#9900CC;">index</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;qwertyuiopasdfghjkl&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;sub:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> str.<span style="color:#CC0066; font-weight:bold;">sub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;ghi&quot;</span>, <span style="color:#996600;">&quot;GGHHII&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;gsub:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> str.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;ghi&quot;</span>, <span style="color:#996600;">&quot;GGHHII&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;[x..y]:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> str<span style="color:#006600; font-weight:bold;">&#91;</span>0..2<span style="color:#006600; font-weight:bold;">&#93;</span>;str<span style="color:#006600; font-weight:bold;">&#91;</span>5..15<span style="color:#006600; font-weight:bold;">&#93;</span>;str<span style="color:#006600; font-weight:bold;">&#91;</span>6..26<span style="color:#006600; font-weight:bold;">&#93;</span>;str<span style="color:#006600; font-weight:bold;">&#91;</span>10..50<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;slice:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> str.<span style="color:#9900CC;">slice</span><span style="color:#006600; font-weight:bold;">&#40;</span>0..2<span style="color:#006600; font-weight:bold;">&#41;</span>;str.<span style="color:#9900CC;">slice</span><span style="color:#006600; font-weight:bold;">&#40;</span>5..15<span style="color:#006600; font-weight:bold;">&#41;</span>;str.<span style="color:#9900CC;">slice</span><span style="color:#006600; font-weight:bold;">&#40;</span>6..26<span style="color:#006600; font-weight:bold;">&#41;</span>;str.<span style="color:#9900CC;">slice</span><span style="color:#006600; font-weight:bold;">&#40;</span>10..50<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;strip:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#996600;">&quot;  1 2 3 4 5  &quot;</span>.<span style="color:#9900CC;">strip</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Each:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> str.<span style="color:#9900CC;">each_line</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x <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;">end</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Cast 1.000.000:&quot;</span>
  n=<span style="color:#006666;">1</span>_000_000
  str=<span style="color:#996600;">&quot;abcd1234&quot;</span>
  t4 = b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;.to_i:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> str.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t4 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;.to_sym:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> str.<span style="color:#9900CC;">to_sym</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  n=<span style="color:#006666;">100</span>_000
  str=<span style="color:#996600;">&quot;abc123 &quot;</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">100</span>
  t4 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;split:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> str.<span style="color:#CC0066; font-weight:bold;">split</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#006600; font-weight:bold;">&#91;</span>t1<span style="color:#006600; font-weight:bold;">+</span>t2<span style="color:#006600; font-weight:bold;">+</span>t3<span style="color:#006600; font-weight:bold;">+</span>t4<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Questi sono i risultati in ordine di interprete, dal primo al quarto:</p>
<p><strong>Ruby 1.8.6</strong></p>
<pre>
C:ProgettiRubybench>ruby bench_str.rb
                           user     system      total        real
Concat 1.000.000:
+                      3.525000   0.000000   3.525000 (  3.555325)
<<                     2.864000   0.000000   2.864000 (  2.884320)
#{}                    3.565000   0.010000   3.575000 (  3.615415)
Add 100.000:
+=                     5.257000   4.306000   9.563000 (  9.724565)
<<                     0.030000   0.010000   0.040000 (  0.040060)
a = a + '.'            5.398000   4.376000   9.774000 (  9.884805)
#{}                    5.328000   2.474000   7.802000 (  7.901835)
Other 100.000:
* 100:                 0.551000   0.380000   0.931000 (  0.981470)
capitalize:            0.951000   0.361000   1.312000 (  1.311965)
upcase:                2.844000   0.490000   3.334000 (  3.365040)
chomp:                 0.491000   0.301000   0.792000 (  0.861290)
include:               0.631000   0.000000   0.631000 (  0.630945)
index:                 0.611000   0.000000   0.611000 (  0.610915)
sub:                   1.181000   0.410000   1.591000 (  1.602400)
gsub:                 19.989000   1.623000  21.612000 ( 21.842715)
[x..y]:                0.351000   0.000000   0.351000 (  0.360540)
slice:                 0.350000   0.000000   0.350000 (  0.350525)
strip:                 0.090000   0.000000   0.090000 (  0.090135)
Each:                  7.711000   0.000000   7.711000 (  7.761625)
Cast 1.000.000:
.to_i:                 0.221000   0.000000   0.221000 (  0.220330)
.to_sym:               0.240000   0.000000   0.240000 (  0.240360)
split:                 6.229000   0.010000   6.239000 (  6.269390)
--- Total:            68.408000  14.751000  83.159000 ( 84.105970)
</pre>
<p><strong>Ruby 1.9.1</strong></p>
<pre>
C:ProgettiRubybench>ruby bench_str.rb
                           user     system      total        real
Concat 1.000.000:
+                      1.932000   0.000000   1.932000 (  1.992985)
<<                     1.633000   0.000000   1.633000 (  1.672505)
#{}                    2.183000   0.000000   2.183000 (  2.213315)
Add 100.000:
+=                     5.538000   4.136000   9.674000 (  9.884805)
<<                     0.030000   0.000000   0.030000 (  0.030045)
a = a + '.'            5.368000   4.346000   9.714000 (  9.884805)
#{}                    6.529000   4.246000  10.775000 ( 11.096620)
Other 100.000:
* 100:                 0.491000   0.400000   0.891000 (  0.891335)
capitalize:            4.406000   0.501000   4.907000 (  4.977455)
upcase:                0.671000   0.501000   1.172000 (  1.171755)
chomp:                 0.541000   0.340000   0.881000 (  0.911365)
include:               0.110000   0.000000   0.110000 (  0.110165)
index:                 0.120000   0.000000   0.120000 (  0.120180)
sub:                   0.731000   0.371000   1.102000 (  1.111665)
gsub:                  7.361000   0.991000   8.352000 (  8.512750)
[x..y]:                0.200000   0.010000   0.210000 (  0.210315)
slice:                 0.170000   0.000000   0.170000 (  0.170255)
strip:                 0.090000   0.000000   0.090000 (  0.090135)
Each:                  6.359000   0.020000   6.379000 (  6.419615)
Cast 1.000.000:
.to_i:                 0.171000   0.000000   0.171000 (  0.170255)
.to_sym:               0.290000   0.000000   0.290000 (  0.330495)
split:                 3.575000   0.000000   3.575000 (  3.595385)
--- Total:            48.499000  15.862000  64.361000 ( 65.568205)
</pre>
<p><strong>JRuby</strong></p>
<pre>
C:ProgettiRubybench>jruby bench_str.rb
                           user     system      total        real
Concat 1.000.000:
+                      1.142000   0.000000   1.142000 (  1.101000)
<<                     0.932000   0.000000   0.932000 (  0.942000)
#{}                    0.430000   0.000000   0.430000 (  0.430000)
Add 100.000:
+=                     7.962000   0.000000   7.962000 (  7.962000)
<<                     0.020000   0.000000   0.020000 (  0.020000)
a = a + '.'            7.542000   0.000000   7.542000 (  7.542000)
#{}                   16.324000   0.000000  16.324000 ( 16.324000)
Other 100.000:
* 100:                 0.140000   0.000000   0.140000 (  0.140000)
capitalize:            0.781000   0.000000   0.781000 (  0.781000)
upcase:                1.382000   0.000000   1.382000 (  1.382000)
chomp:                 0.051000   0.000000   0.051000 (  0.051000)
include:               0.240000   0.000000   0.240000 (  0.240000)
index:                 0.230000   0.000000   0.230000 (  0.230000)
sub:                   0.381000   0.000000   0.381000 (  0.391000)
gsub:                  3.816000   0.000000   3.816000 (  3.816000)
[x..y]:                0.100000   0.000000   0.100000 (  0.100000)
slice:                 0.120000   0.000000   0.120000 (  0.120000)
strip:                 0.040000   0.000000   0.040000 (  0.040000)
Each:                  2.333000   0.000000   2.333000 (  2.333000)
Cast 1.000.000:
.to_i:                 0.470000   0.000000   0.470000 (  0.470000)
.to_sym:               0.191000   0.000000   0.191000 (  0.191000)
split:                 1.622000   0.000000   1.622000 (  1.622000)
--- Total:            46.249000   0.000000  46.249000 ( 46.227999)
</pre>
<p><strong>IronRuby</strong></p>
<pre>
C:ProgettiRubybench>ir bench_str.rb
                           user     system      total        real
Concat 1.000.000:
+                      2.804032   0.070101   2.874133 (  2.954425)
<<                     1.101584   0.000000   1.101584 (  1.101650)
#{}                    1.331915   0.000000   1.331915 (  1.342010)
Add 100.000:
+=                    50.502619   4.836955  55.339574 ( 56.043940)
<<                     0.040058   0.000000   0.040058 (  0.040060)
a = a + '.'           50.662850   4.616638  55.279488 ( 56.013895)
#{}                   51.173584   4.666710  55.840294 ( 56.634825)
Other 100.000:
* 100:                 0.500720   0.000000   0.500720 (  0.500750)
capitalize:            2.503600   0.010014   2.513614 (  2.583870)
upcase:                6.078741   0.000000   6.078741 (  6.199285)
chomp:                 0.200288   0.000000   0.200288 (  0.200300)
include:               4.216062   0.000000   4.216062 (  4.246360)
index:                 4.135947   0.000000   4.135947 (  4.206300)
sub:                   1.462102   0.000000   1.462102 (  1.492235)
gsub:                  8.692499   0.020029   8.712528 (  8.773140)
[x..y]:                0.290418   0.000000   0.290418 (  0.290435)
slice:                 0.230331   0.010014   0.240346 (  0.260390)
strip:                 0.070101   0.000000   0.070101 (  0.070105)
Each:                 25.015971   0.050072  25.066043 ( 25.357980)
Cast 1.000.000:
.to_i:                 0.220317   0.000000   0.220317 (  0.220330)
.to_sym:               0.280403   0.000000   0.280403 (  0.280420)
split:                 4.446394   0.010014   4.456408 (  4.516765)
--- Total:           215.960536  14.290549 230.251085 (233.329469)
</pre>
<p>Questo è il <strong>riepilogo dei totali</strong>:<br />
<code><br />
Ruby 1.8.6<br />
--- Total:            68.408000  14.751000  83.159000 ( 84.105970)<br />
Ruby 1.9.1<br />
--- Total:            48.499000  15.862000  64.361000 ( 65.568205)<br />
Jruby<br />
--- Total:            46.249000   0.000000  46.249000 <strong>( 46.227999)</strong><br />
Ironruby<br />
--- Total:           215.960536  14.290549 230.251085 (233.329469)<br />
</code></p>
<p><strong>Commento</strong>: Per la concatenazione, in tutti i casi prevale l'operatore << mentre includere le variabili nelle stringhe con #{}, sarà forse efficiente come uso di memoria ma in quanto a prestazioni è in coda agli altri. Ironruby fatica con le stringhe, è alto infatti il divario con particolari operatori anche se basterebbe usare << per quasi annullare il divario. Un pò di fatica anche sul ciclo delle righe se confrontato con gli altri tre. Molto bene ruby 1.9.1 con prestazioni simili a Jruby ma un consumo di memoria di soli 5Mb contro 30 della versione java. Anche la versione 1.8.6 non è niente male, ha perso tempo con gsub (soprattutto rispetto a jruby!).</p>
<h3>I numeri</h3>
<p>Lo script:</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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;benchmark&quot;</span>
<span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#CC00FF; font-weight:bold;">Benchmark</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">Benchmark</span>.<span style="color:#9900CC;">bm</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">21</span>, <span style="color:#996600;">&quot;--- Total:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><span style="color:#006600; font-weight:bold;">|</span>b<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Integer&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Arithmetic operations 10.000.000:&quot;</span>
  n=<span style="color:#006666;">10</span>_000_000
  t1 = b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;x + 2&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#006666;">0</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> a = x <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;x - 1&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> a = x <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;x * 3&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> a = x <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">3</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;x / 2&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> a = x <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;x ** 2&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> a = x <span style="color:#006600; font-weight:bold;">**</span> <span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;x % 360&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">360</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Cast&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x.<span style="color:#9900CC;">to_f</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Add 10.000.000:&quot;</span>
  n=<span style="color:#006666;">10</span>_000_000
  t2 = b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;+=&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#006666;">0</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t2 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;a = a + 1&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#006666;">0</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a = a <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t2 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;&lt;&lt;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#006666;">0</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t2 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;.next&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#006666;">0</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a = a.<span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Float&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Arithmetic operations 10.000.000:&quot;</span>
  n=<span style="color:#006666;">10</span>_000_000
  t3 = b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;x + 1.234567&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#006666;">0</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> a = x <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1.234567</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;x - 0.135799&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#006666;">0</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> a = x <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">0.135799</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;x * 0.987654&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#006666;">0</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> a = x <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">0.987654</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;x / 1.975313&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#006666;">0</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> a = x <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#006666;">1.975313</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;x ** 1.987654&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#006666;">0</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> a = x <span style="color:#006600; font-weight:bold;">**</span> <span style="color:#006666;">1.987654</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Cast&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#006600; font-weight:bold;">&#91;</span>t1<span style="color:#006600; font-weight:bold;">+</span>t2<span style="color:#006600; font-weight:bold;">+</span>t3<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p><strong>Ruby 1.8.6</strong></p>
<pre>
C:ProgettiRubybench>ruby bench_num.rb
                           user     system      total        real
Integer
Arithmetic operations 10.000.000:
x + 2                  3.154000   0.000000   3.154000 (  3.194785)
x - 1                  3.365000   0.000000   3.365000 (  3.415115)
x * 3                  3.415000   0.000000   3.415000 (  3.495235)
x / 2                  3.475000   0.000000   3.475000 (  3.495235)
x ** 2                14.771000   0.010000  14.781000 ( 14.952395)
x % 360                2.914000   0.000000   2.914000 (  3.004500)
Cast                  11.847000   0.000000  11.847000 ( 11.987955)
Add 10.000.000:
+=                     2.544000   0.000000   2.544000 (  2.583870)
a = a + 1              2.524000   0.000000   2.524000 (  2.533795)
<<                     2.233000   0.000000   2.233000 (  2.253375)
.next                  1.963000   0.000000   1.963000 (  1.982970)
Float
Arithmetic operations 10.000.000:
x + 1.234567           3.885000   0.000000   3.885000 (  3.985970)
x - 0.135799           3.886000   0.000000   3.886000 (  3.945910)
x * 0.987654           3.895000   0.000000   3.895000 (  3.945910)
x / 1.975313          12.378000   0.010000  12.388000 ( 12.588855)
x ** 1.987654          5.057000   0.000000   5.057000 (  5.147710)
Cast                  11.106000   0.030000  11.136000 ( 11.286905)
--- Total:            92.412000   0.050000  92.462000 ( 93.800490)
</pre>
<p><strong>Ruby 1.9.1</strong></p>
<pre>
C:ProgettiRubybench>ruby bench_num.rb
                           user     system      total        real
Integer
Arithmetic operations 10.000.000:
x + 2                  0.981000   0.000000   0.981000 (  0.991485)
x - 1                  0.901000   0.000000   0.901000 (  0.911365)
x * 3                  0.962000   0.000000   0.962000 (  0.961440)
x / 2                  0.911000   0.000000   0.911000 (  0.921380)
x ** 2                 4.316000   0.000000   4.316000 (  4.366540)
x % 360                0.881000   0.000000   0.881000 (  0.961440)
Cast                   4.727000   0.020000   4.747000 (  5.217815)
Add 10.000.000:
+=                     0.952000   0.000000   0.952000 (  0.951425)
a = a + 1              0.901000   0.010000   0.911000 (  0.951425)
<<                     1.312000   0.000000   1.312000 (  1.342010)
.next                  1.312000   0.020000   1.332000 (  1.402100)
Float
Arithmetic operations 10.000.000:
x + 1.234567           2.103000   0.000000   2.103000 (  2.233345)
x - 0.135799           1.983000   0.000000   1.983000 (  2.013015)
x * 0.987654           2.042000   0.000000   2.042000 (  2.053075)
x / 1.975313           2.183000   0.000000   2.183000 (  2.243360)
x ** 1.987654          3.174000   0.000000   3.174000 (  3.184770)
Cast                   4.106000   0.000000   4.106000 (  4.146210)
--- Total:            33.747000   0.050000  33.797000 ( 34.852200)
</pre>
<p><strong>JRuby</strong></p>
<pre>
C:ProgettiRubybench>jruby bench_num.rb
                           user     system      total        real
Integer
Arithmetic operations 10.000.000:
x + 2                  2.043000   0.000000   2.043000 (  1.863000)
x - 1                  1.803000   0.000000   1.803000 (  1.803000)
x * 3                  1.923000   0.000000   1.923000 (  1.923000)
x / 2                  2.153000   0.000000   2.153000 (  2.153000)
x ** 2                 5.899000   0.000000   5.899000 (  5.899000)
x % 360                1.803000   0.000000   1.803000 (  1.803000)
Cast                   4.527000   0.000000   4.527000 (  4.527000)
Add 10.000.000:
+=                     1.682000   0.000000   1.682000 (  1.682000)
a = a + 1              1.602000   0.000000   1.602000 (  1.602000)
<<                     1.302000   0.000000   1.302000 (  1.302000)
.next                  1.433000   0.000000   1.433000 (  1.433000)
Float
Arithmetic operations 10.000.000:
x + 1.234567           2.363000   0.000000   2.363000 (  2.363000)
x - 0.135799           2.404000   0.000000   2.404000 (  2.404000)
x * 0.987654           2.153000   0.000000   2.153000 (  2.153000)
x / 1.975313           4.487000   0.000000   4.487000 (  4.487000)
x ** 1.987654          5.829000   0.000000   5.829000 (  5.839000)
Cast                   4.146000   0.000000   4.146000 (  4.156000)
--- Total:            47.552000   0.000000  47.552000 ( 47.392000)
</pre>
<p><strong>IronRuby</strong></p>
<pre>
C:ProgettiRubybench>ir bench_num.rb
                           user     system      total        real
Integer
Arithmetic operations 10.000.000:
x + 2                  3.925645   0.060086   3.985731 (  4.056075)
x - 1                  1.652376   0.000000   1.652376 (  1.662490)
x * 3                  1.912750   0.000000   1.912750 (  1.972955)
x / 2                  1.982851   0.010014   1.992866 (  2.013015)
x ** 2                 3.895602   0.010014   3.905616 (  3.975955)
x % 360                1.672405   0.010014   1.682419 (  1.692535)
Cast                   6.248986   0.010014   6.259000 (  6.389570)
Add 10.000.000:
+=                     1.672405   0.010014   1.682419 (  1.712565)
a = a + 1              1.742506   0.000000   1.742506 (  1.792685)
<<                     1.472117   0.040058   1.512174 (  1.532295)
.next                  1.672405   0.000000   1.672405 (  1.672505)
Float
Arithmetic operations 10.000.000:
x + 1.234567           1.932779   0.000000   1.932779 (  1.932895)
x - 0.135799           1.952808   0.010014   1.962822 (  2.033045)
x * 0.987654           1.992866   0.000000   1.992866 (  1.992985)
x / 1.975313           3.825501   0.030043   3.855544 (  3.925880)
x ** 1.987654          3.014334   0.000000   3.014334 (  3.034545)
Cast                   5.878453   0.000000   5.878453 (  5.928880)
--- Total:            46.446787   0.190274  46.637061 ( 47.320875)
</pre>
<p><strong>Riepilogo totali</strong>:<br />
<code><br />
Ruby 1.8.6<br />
--- Total:            92.412000   0.050000  92.462000 ( 93.800490)<br />
Ruby 1.9.1<br />
--- Total:            33.747000   0.050000  33.797000 <strong>( 34.852200)</strong><br />
Jruby<br />
--- Total:            47.552000   0.000000  47.552000 ( 47.392000)<br />
Ironruby<br />
--- Total:            46.446787   0.190274  46.637061 ( 47.320875)<br />
</code></p>
<p><strong>Commento</strong>: Netto miglioramento della versione 1.9.1 che, con i numeri, è tre volte più veloce della 1.8.6. Anche gli altri due non sono male.<br />
<em><strong>16/08/2009</strong>: Mi sono accorto di aver inserito erroneamente il metodo shift (<<) tra gli add.</em><br />
<em><strong>24/08/2009</strong>: Avevo invertito i risultati della 1.8.6 con quelli della 1.9.1... in effetti mi sembravano un pò strani :-p</em></p>
<h3>Gli array</h3>
<p>Lo script:</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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;benchmark&quot;</span>
<span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#CC00FF; font-weight:bold;">Benchmark</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">Benchmark</span>.<span style="color:#9900CC;">bm</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">21</span>, <span style="color:#996600;">&quot;Total:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><span style="color:#006600; font-weight:bold;">|</span>b<span style="color:#006600; font-weight:bold;">|</span>
  n=<span style="color:#006666;">100</span>_000
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Create 100.000:&quot;</span>
  t1 = b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;%w()&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span>a b c d f e g h j k i l m n o <span style="color:#CC0066; font-weight:bold;">p</span> q r s t u v w y z <span style="color:#006666;">0</span> <span style="color:#006666;">1</span> <span style="color:#006666;">2</span> <span style="color:#006666;">3</span> <span style="color:#006666;">4</span> <span style="color:#006666;">5</span> <span style="color:#006666;">6</span> <span style="color:#006666;">7</span> <span style="color:#006666;">8</span> <span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;%w''&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#996600;">&quot;a b c d f e g h j k i l m n o p q r s t u v w y z 0 1 2 3 4 5 6 7 8 9&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;split&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;a b c d f e g h j k i l m n o p q r s t u v w y z 0 1 2 3 4 5 6 7 8 9&quot;</span>.<span style="color:#CC0066; font-weight:bold;">split</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:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;[str]&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;a&quot;</span>,<span style="color:#996600;">&quot;b&quot;</span>,<span style="color:#996600;">&quot;c&quot;</span>,<span style="color:#996600;">&quot;d&quot;</span>,<span style="color:#996600;">&quot;f&quot;</span>,<span style="color:#996600;">&quot;e&quot;</span>,<span style="color:#996600;">&quot;g&quot;</span>,<span style="color:#996600;">&quot;h&quot;</span>,<span style="color:#996600;">&quot;j&quot;</span>,<span style="color:#996600;">&quot;k&quot;</span>,<span style="color:#996600;">&quot;i&quot;</span>,<span style="color:#996600;">&quot;l&quot;</span>,<span style="color:#996600;">&quot;m&quot;</span>,<span style="color:#996600;">&quot;n&quot;</span>,<span style="color:#996600;">&quot;o&quot;</span>,<span style="color:#996600;">&quot;p&quot;</span>,<span style="color:#996600;">&quot;q&quot;</span>,<span style="color:#996600;">&quot;r&quot;</span>,<span style="color:#996600;">&quot;s&quot;</span>,<span style="color:#996600;">&quot;t&quot;</span>,<span style="color:#996600;">&quot;u&quot;</span>,<span style="color:#996600;">&quot;v&quot;</span>,<span style="color:#996600;">&quot;w&quot;</span>,<span style="color:#996600;">&quot;y&quot;</span>,<span style="color:#996600;">&quot;z&quot;</span>,<span style="color:#996600;">&quot;0&quot;</span>,<span style="color:#996600;">&quot;1&quot;</span>,<span style="color:#996600;">&quot;2&quot;</span>,<span style="color:#996600;">&quot;3&quot;</span>,<span style="color:#996600;">&quot;4&quot;</span>,<span style="color:#996600;">&quot;5&quot;</span>,<span style="color:#996600;">&quot;6&quot;</span>,<span style="color:#996600;">&quot;7&quot;</span>,<span style="color:#996600;">&quot;8&quot;</span>,<span style="color:#996600;">&quot;9&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;[num]&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span>,<span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">5</span>,<span style="color:#006666;">6</span>,<span style="color:#006666;">7</span>,<span style="color:#006666;">8</span>,<span style="color:#006666;">9</span>,<span style="color:#006666;">0</span>,<span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">5</span>,<span style="color:#006666;">6</span>,<span style="color:#006666;">7</span>,<span style="color:#006666;">8</span>,<span style="color:#006666;">9</span>,<span style="color:#006666;">0</span>,<span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">5</span>,<span style="color:#006666;">6</span>,<span style="color:#006666;">7</span>,<span style="color:#006666;">8</span>,<span style="color:#006666;">9</span>,<span style="color:#006666;">0</span>,<span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Array.new(str)&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">35</span>, <span style="color:#996600;">&quot;a&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Array.new(num)&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">35</span>, <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Array.new(hash)&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">35</span>, <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  n=<span style="color:#006666;">100</span>_000
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Add 100.000:&quot;</span>
  t2 = b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;+= ['.']&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;.&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  t2 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;+= [0]&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  t2 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;&lt;&lt; ['.']&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;.&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  t2 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;&lt;&lt; [0]&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  t2 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;a = a+['.']&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a = a <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;.&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  t2 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;a = a+[0]&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a = a <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  n=<span style="color:#006666;">100</span>_000
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Concatenate 100.000:&quot;</span>
  t3 = b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;[num]+&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span>,<span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">5</span>,<span style="color:#006666;">6</span>,<span style="color:#006666;">7</span>,<span style="color:#006666;">8</span>,<span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span>,<span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">5</span>,<span style="color:#006666;">6</span>,<span style="color:#006666;">7</span>,<span style="color:#006666;">8</span>,<span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;[str]+&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;a&quot;</span>,<span style="color:#996600;">&quot;b&quot;</span>,<span style="color:#996600;">&quot;c&quot;</span>,<span style="color:#996600;">&quot;d&quot;</span>,<span style="color:#996600;">&quot;f&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;e&quot;</span>,<span style="color:#996600;">&quot;g&quot;</span>,<span style="color:#996600;">&quot;h&quot;</span>,<span style="color:#996600;">&quot;j&quot;</span>,<span style="color:#996600;">&quot;k&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;i&quot;</span>,<span style="color:#996600;">&quot;l&quot;</span>,<span style="color:#996600;">&quot;m&quot;</span>,<span style="color:#996600;">&quot;n&quot;</span>,<span style="color:#996600;">&quot;o&quot;</span>,<span style="color:#996600;">&quot;p&quot;</span>,<span style="color:#996600;">&quot;q&quot;</span>,<span style="color:#996600;">&quot;r&quot;</span>,<span style="color:#996600;">&quot;s&quot;</span>,<span style="color:#996600;">&quot;t&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;[mix]+&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;a&quot;</span>,<span style="color:#996600;">&quot;b&quot;</span>,<span style="color:#996600;">&quot;c&quot;</span>,<span style="color:#996600;">&quot;d&quot;</span>,<span style="color:#996600;">&quot;f&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">5</span>,<span style="color:#006666;">6</span>,<span style="color:#006666;">7</span>,<span style="color:#006666;">8</span>,<span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;i&quot;</span>,<span style="color:#006666;">0</span>,<span style="color:#996600;">&quot;m&quot;</span>,<span style="color:#006666;">1</span>,<span style="color:#996600;">&quot;o&quot;</span>,<span style="color:#006666;">2</span>,<span style="color:#996600;">&quot;q&quot;</span>,<span style="color:#006666;">3</span>,<span style="color:#996600;">&quot;s&quot;</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;[num]&lt;&lt;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span>,<span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&lt;&lt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">5</span>,<span style="color:#006666;">6</span>,<span style="color:#006666;">7</span>,<span style="color:#006666;">8</span>,<span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&lt;&lt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span>,<span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">5</span>,<span style="color:#006666;">6</span>,<span style="color:#006666;">7</span>,<span style="color:#006666;">8</span>,<span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;[str]&lt;&lt;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;a&quot;</span>,<span style="color:#996600;">&quot;b&quot;</span>,<span style="color:#996600;">&quot;c&quot;</span>,<span style="color:#996600;">&quot;d&quot;</span>,<span style="color:#996600;">&quot;f&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&lt;&lt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;e&quot;</span>,<span style="color:#996600;">&quot;g&quot;</span>,<span style="color:#996600;">&quot;h&quot;</span>,<span style="color:#996600;">&quot;j&quot;</span>,<span style="color:#996600;">&quot;k&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&lt;&lt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;i&quot;</span>,<span style="color:#996600;">&quot;l&quot;</span>,<span style="color:#996600;">&quot;m&quot;</span>,<span style="color:#996600;">&quot;n&quot;</span>,<span style="color:#996600;">&quot;o&quot;</span>,<span style="color:#996600;">&quot;p&quot;</span>,<span style="color:#996600;">&quot;q&quot;</span>,<span style="color:#996600;">&quot;r&quot;</span>,<span style="color:#996600;">&quot;s&quot;</span>,<span style="color:#996600;">&quot;t&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;[mix]&lt;&lt;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;a&quot;</span>,<span style="color:#996600;">&quot;b&quot;</span>,<span style="color:#996600;">&quot;c&quot;</span>,<span style="color:#996600;">&quot;d&quot;</span>,<span style="color:#996600;">&quot;f&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&lt;&lt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">5</span>,<span style="color:#006666;">6</span>,<span style="color:#006666;">7</span>,<span style="color:#006666;">8</span>,<span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&lt;&lt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;i&quot;</span>,<span style="color:#006666;">0</span>,<span style="color:#996600;">&quot;m&quot;</span>,<span style="color:#006666;">1</span>,<span style="color:#996600;">&quot;o&quot;</span>,<span style="color:#006666;">2</span>,<span style="color:#996600;">&quot;q&quot;</span>,<span style="color:#006666;">3</span>,<span style="color:#996600;">&quot;s&quot;</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
   t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;[num].concat&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span>,<span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">concat</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">5</span>,<span style="color:#006666;">6</span>,<span style="color:#006666;">7</span>,<span style="color:#006666;">8</span>,<span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">concat</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span>,<span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">5</span>,<span style="color:#006666;">6</span>,<span style="color:#006666;">7</span>,<span style="color:#006666;">8</span>,<span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;[str].concat&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;a&quot;</span>,<span style="color:#996600;">&quot;b&quot;</span>,<span style="color:#996600;">&quot;c&quot;</span>,<span style="color:#996600;">&quot;d&quot;</span>,<span style="color:#996600;">&quot;f&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">concat</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;e&quot;</span>,<span style="color:#996600;">&quot;g&quot;</span>,<span style="color:#996600;">&quot;h&quot;</span>,<span style="color:#996600;">&quot;j&quot;</span>,<span style="color:#996600;">&quot;k&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">concat</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;i&quot;</span>,<span style="color:#996600;">&quot;l&quot;</span>,<span style="color:#996600;">&quot;m&quot;</span>,<span style="color:#996600;">&quot;n&quot;</span>,<span style="color:#996600;">&quot;o&quot;</span>,<span style="color:#996600;">&quot;p&quot;</span>,<span style="color:#996600;">&quot;q&quot;</span>,<span style="color:#996600;">&quot;r&quot;</span>,<span style="color:#996600;">&quot;s&quot;</span>,<span style="color:#996600;">&quot;t&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;[mix].concat&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;a&quot;</span>,<span style="color:#996600;">&quot;b&quot;</span>,<span style="color:#996600;">&quot;c&quot;</span>,<span style="color:#996600;">&quot;d&quot;</span>,<span style="color:#996600;">&quot;f&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">concat</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">5</span>,<span style="color:#006666;">6</span>,<span style="color:#006666;">7</span>,<span style="color:#006666;">8</span>,<span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">concat</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;i&quot;</span>,<span style="color:#006666;">0</span>,<span style="color:#996600;">&quot;m&quot;</span>,<span style="color:#006666;">1</span>,<span style="color:#996600;">&quot;o&quot;</span>,<span style="color:#006666;">2</span>,<span style="color:#996600;">&quot;q&quot;</span>,<span style="color:#006666;">3</span>,<span style="color:#996600;">&quot;s&quot;</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;union [num]&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span>,<span style="color:#006666;">14</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">53</span>,<span style="color:#006666;">682</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">53</span>,<span style="color:#006666;">6</span>,<span style="color:#006666;">7</span>,<span style="color:#006666;">84</span>,<span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;union [str]&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;a&quot;</span>,<span style="color:#996600;">&quot;b&quot;</span>,<span style="color:#996600;">&quot;c&quot;</span>,<span style="color:#996600;">&quot;d&quot;</span>,<span style="color:#996600;">&quot;f&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;b&quot;</span>,<span style="color:#996600;">&quot;d&quot;</span>,<span style="color:#996600;">&quot;h&quot;</span>,<span style="color:#996600;">&quot;j&quot;</span>,<span style="color:#996600;">&quot;k&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  a=<span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">100</span>, <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  n=<span style="color:#006666;">1</span>_000_000
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Read 1.000.000:&quot;</span>
  t4 = b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;each:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x<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;">end</span>
  t4 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;map:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x<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;">end</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Other 10.000:&quot;</span>
  n=<span style="color:#006666;">10</span>_000
  a=<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;a&quot;</span>,<span style="color:#0000FF; font-weight:bold;">nil</span>,<span style="color:#006666;">0</span>,<span style="color:#996600;">&quot;b&quot;</span>,<span style="color:#006666;">1</span>,<span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">500</span>
  t5 = b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;.compact:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a.<span style="color:#9900CC;">compact</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t5 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;.delete(nil):&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> a.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><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;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  a=<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> a <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> x <span style="color:#006600; font-weight:bold;">&#125;</span>
  t5 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;delete:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> a.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;-&quot;</span><span style="color:#006600; font-weight:bold;">*</span><span style="color:#006666;">20</span>
  <span style="color:#006600; font-weight:bold;">&#91;</span>t1<span style="color:#006600; font-weight:bold;">+</span>t2<span style="color:#006600; font-weight:bold;">+</span>t3<span style="color:#006600; font-weight:bold;">+</span>t4<span style="color:#006600; font-weight:bold;">+</span>t5<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p><strong>Ruby 1.8.6</strong></p>
<pre>
C:ProgettiRubybench>ruby bench_arr.rb
                           user     system      total        real
Create 100.000:
%w()                   0.300000   0.010000   0.310000 (  0.320480)
%w''                   0.300000   0.000000   0.300000 (  0.310465)
split                  2.164000   0.010000   2.174000 (  2.213315)
[str]                  0.310000   0.000000   0.310000 (  0.310465)
[num]                  0.160000   0.030000   0.190000 (  0.220330)
Array.new(str)         0.180000   0.010000   0.190000 (  0.190285)
Array.new(num)         0.151000   0.030000   0.181000 (  0.180270)
Array.new(hash)        0.270000   0.040000   0.310000 (  0.310465)
Add 100.000:
+= ['.']              22.472000   8.272000  30.744000 ( 31.487160)
+= [0]                20.800000   8.602000  29.402000 ( 30.004940)
<< ['.']               0.070000   0.010000   0.080000 (  0.090135)
<< [0]                 0.070000   0.000000   0.070000 (  0.070105)
a = a+['.']           28.321000   7.351000  35.672000 ( 36.514690)
a = a+[0]             25.226000   7.480000  32.706000 ( 33.520205)
Concatenate 100.000:
[num]+                 0.451000   0.030000   0.481000 (  0.490735)
[str]+                 0.581000   0.030000   0.611000 (  0.640960)
[mix]+                 0.521000   0.030000   0.551000 (  0.550825)
[num]<<                0.350000   0.020000   0.370000 (  0.370555)
[str]<<                0.421000   0.031000   0.452000 (  0.450675)
[mix]<<                0.420000   0.010000   0.430000 (  0.430645)
[num].concat           0.341000   0.030000   0.371000 (  0.380570)
[str].concat           0.491000   0.020000   0.511000 (  0.510765)
[mix].concat           0.400000   0.020000   0.420000 (  0.430645)
union [num]            1.032000   0.020000   1.052000 (  1.051575)
union [str]            0.761000   0.080000   0.841000 (  0.881320)
Read 1.000.000:
each:                 16.934000   0.000000  16.934000 ( 17.145680)
map:                  20.750000   1.181000  21.931000 ( 22.243315)
Other 10.000:
.compact:              0.401000   0.131000   0.532000 (  0.530795)
.delete(nil):          4.246000   0.000000   4.246000 (  4.316465)
delete:                4.937000   0.000000   4.937000 (  5.017515)
--------------------
Total:               153.831000  33.478000 187.309000 (191.186350)
</pre>
<p><strong>Ruby 1.9.1</strong></p>
<pre>
C:ProgettiRubybench>ruby bench_arr.rb
                           user     system      total        real
Create 100.000:
%w()                   0.520000   0.000000   0.520000 (  0.520780)
%w''                   0.541000   0.000000   0.541000 (  0.550825)
split                  0.781000   0.000000   0.781000 (  0.791185)
[str]                  0.511000   0.000000   0.511000 (  0.520780)
[num]                  0.020000   0.000000   0.020000 (  0.020030)
Array.new(str)         0.170000   0.010000   0.180000 (  0.190285)
Array.new(num)         0.140000   0.000000   0.140000 (  0.140210)
Array.new(hash)        0.141000   0.030000   0.171000 (  0.200300)
Add 100.000:
+= ['.']              22.131000   9.553000  31.684000 ( 32.268330)
+= [0]                16.514000   8.422000  24.936000 ( 25.478160)
<< ['.']               0.010000   0.020000   0.030000 (  0.030045)
<< [0]                 0.030000   0.000000   0.030000 (  0.030045)
a = a+['.']           17.856000   3.946000  21.802000 ( 22.323435)
a = a+[0]             17.024000   8.172000  25.196000 ( 25.788625)
Concatenate 100.000:
[num]+                 0.211000   0.020000   0.231000 (  0.230345)
[str]+                 0.490000   0.000000   0.490000 (  0.500750)
[mix]+                 0.341000   0.000000   0.341000 (  0.340510)
[num]<<                0.140000   0.000000   0.140000 (  0.140210)
[str]<<                0.421000   0.010000   0.431000 (  0.460690)
[mix]<<                0.260000   0.000000   0.260000 (  0.260390)
[num].concat           0.160000   0.000000   0.160000 (  0.160240)
[str].concat           0.471000   0.000000   0.471000 (  0.470705)
[mix].concat           0.320000   0.000000   0.320000 (  0.320480)
union [num]            0.691000   0.020000   0.711000 (  0.721080)
union [str]            0.952000   0.000000   0.952000 (  0.961440)
Read 1.000.000:
each:                  7.861000   0.000000   7.861000 (  7.991970)
map:                  13.169000   1.482000  14.651000 ( 14.822200)
Other 10.000:
.compact:              0.250000   0.130000   0.380000 (  0.380570)
.delete(nil):          2.684000   0.000000   2.684000 (  2.704050)
delete:                4.907000   0.000000   4.907000 (  4.987470)
--------------------
Total:               109.717000  31.815000 141.532000 (144.306135)
</pre>
<p><strong>JRuby</strong></p>
<pre>
C:ProgettiRubybench>jruby bench_arr.rb
                           user     system      total        real
Create 100.000:
%w()                   0.251000   0.000000   0.251000 (  0.211000)
%w''                   0.190000   0.000000   0.190000 (  0.190000)
split                  0.370000   0.000000   0.370000 (  0.370000)
[str]                  0.191000   0.000000   0.191000 (  0.191000)
[num]                  0.030000   0.000000   0.030000 (  0.030000)
Array.new(str)         0.080000   0.000000   0.080000 (  0.080000)
Array.new(num)         0.060000   0.000000   0.060000 (  0.060000)
Array.new(hash)        0.100000   0.000000   0.100000 (  0.100000)
Add 100.000:
+= ['.']             143.074000   0.000000 143.074000 (143.074000)
+= [0]               126.099000   0.000000 126.099000 (126.099000)
<< ['.']               0.211000   0.000000   0.211000 (  0.211000)
<< [0]                 0.090000   0.000000   0.090000 (  0.090000)
a = a+['.']          124.536000   0.000000 124.536000 (124.536000)
a = a+[0]            127.672000   0.000000 127.672000 (127.672000)
Concatenate 100.000:
[num]+                 0.120000   0.000000   0.120000 (  0.120000)
[str]+                 0.180000   0.000000   0.180000 (  0.180000)
[mix]+                 0.120000   0.000000   0.120000 (  0.120000)
[num]<<                0.070000   0.000000   0.070000 (  0.070000)
[str]<<                0.211000   0.000000   0.211000 (  0.211000)
[mix]<<                0.110000   0.000000   0.110000 (  0.110000)
[num].concat           0.100000   0.000000   0.100000 (  0.100000)
[str].concat           0.160000   0.000000   0.160000 (  0.160000)
[mix].concat           0.131000   0.000000   0.131000 (  0.131000)
union [num]            0.280000   0.000000   0.280000 (  0.280000)
union [str]            0.300000   0.000000   0.300000 (  0.300000)
Read 1.000.000:
each:                  9.364000   0.000000   9.364000 (  9.364000)
map:                  11.007000   0.000000  11.007000 ( 11.007000)
Other 10.000:
.compact:              0.411000   0.000000   0.411000 (  0.411000)
.delete(nil):          2.904000   0.000000   2.904000 (  2.904000)
delete:                4.687000   0.000000   4.687000 (  4.687000)
--------------------
Total:               553.109000   0.000000 553.109000 (553.069000)
</pre>
<p><strong>IronRuby</strong></p>
<pre>
C:ProgettiRubybench>ir bench_arr.rb
                           user     system      total        real
Create 100.000:
%w()                   0.721037   0.000000   0.721037 (  0.721080)
%w''                   0.230331   0.000000   0.230331 (  0.250375)
split                  1.482131   0.000000   1.482131 (  1.482220)
[str]                  0.240346   0.000000   0.240346 (  0.240360)
[num]                  0.100144   0.000000   0.100144 (  0.100150)
Array.new(str)         0.150216   0.000000   0.150216 (  0.150225)
Array.new(num)         0.130187   0.000000   0.130187 (  0.130195)
Array.new(hash)        0.110158   0.000000   0.110158 (  0.120180)
Add 100.000:
+= ['.']              93.234064   9.723982 102.958046 (105.808475)
+= [0]                44.143475   3.294738  47.438213 ( 48.092030)
<< ['.']               0.160230   0.000000   0.160230 (  0.160240)
<< [0]                 0.090130   0.010014   0.100144 (  0.100150)
a = a+['.']           96.779162   9.483637 106.262798 (109.644220)
a = a+[0]             43.823014   3.545098  47.368112 ( 47.921775)
Concatenate 100.000:
[num]+                 0.160230   0.000000   0.160230 (  0.160240)
[str]+                 0.230331   0.000000   0.230331 (  0.240360)
[mix]+                 0.180259   0.000000   0.180259 (  0.180270)
[num]<<                0.090130   0.000000   0.090130 (  0.090135)
[str]<<                0.180259   0.010014   0.190274 (  0.190285)
[mix]<<                0.150216   0.000000   0.150216 (  0.150225)
[num].concat           0.110158   0.010014   0.120173 (  0.120180)
[str].concat           0.220317   0.000000   0.220317 (  0.220330)
[mix].concat           0.180259   0.000000   0.180259 (  0.180270)
union [num]            0.330475   0.000000   0.330475 (  0.340510)
union [str]            0.410590   0.000000   0.410590 (  0.410615)
Read 1.000.000:
each:                 14.040189   0.010014  14.050203 ( 14.181240)
map:                  19.848541   0.020029  19.868570 ( 20.120135)
Other 10.000:
.compact:              1.161670   0.000000   1.161670 (  1.171755)
.delete(nil):         15.462234   0.030043  15.492277 ( 15.833715)
delete:                1.422045   0.000000   1.422045 (  1.472205)
--------------------
Total:               335.572530  26.137584 361.710114 (369.984144)
</pre>
<p><strong>Riepilogo totali</strong>:<br />
<code><br />
Ruby 1.8.6<br />
Total:               153.831000  33.478000 187.309000 (191.186350)<br />
Ruby 1.9.1<br />
Total:               109.717000  31.815000 141.532000 <strong>(144.306135)</strong><br />
Jruby<br />
Total:               553.109000   0.000000 553.109000 (553.069000)<br />
Ironruby<br />
Total:               335.572530  26.137584 361.710114 (369.984144)<br />
</code></p>
<p><strong>Commento</strong>: Dunque, in questo terzo test è importante spendere qualche parola. Il tempo alto di jruby ed ironruby è dovuto anche al fatto che ho voluto comparare la concatenazione <strong>+=</strong> ed una versione simile, entrambi MOLTO sconvenienti per questi due interpreti, i tempi sono infatti altissimi, basterebbe utilizzare solo l'operatore <strong><<</strong> per avere risultati molto diversi, in ordine: 1.8.6=61 1.9.1=40 jruby=33 e ironruby=60 ...jruby risulterebbe il più veloce.<br />
Un altra curiosità è l'utilizzo del metodo compact, oltre ad essere più elegante è anche più prestazionale del sistema analogo delete(nil) è anche più prestazionale con sensibili differenze in Ironruby.</p>
<h3>Gli Hash</h3>
<p>Lo script per testare gli Hash è il più pesante nell'utilizzo della memoria, sinceramente non era nelle mie intenzioni ma poi ho pensato di tenerlo così per esaminarne gli esiti:</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
58
59
60
61
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;benchmark&quot;</span>
<span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#CC00FF; font-weight:bold;">Benchmark</span>
&nbsp;
n=<span style="color:#006666;">1</span>_000_000
<span style="color:#CC00FF; font-weight:bold;">Benchmark</span>.<span style="color:#9900CC;">bm</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">21</span>, <span style="color:#996600;">&quot;Total:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><span style="color:#006600; font-weight:bold;">|</span>b<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Write 1.000.000:&quot;</span>
  h = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  t1 = b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;int =&gt; str:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;.&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  h = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;int =&gt; int:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span> = x <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;int =&gt; [int]:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  t1 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;.to_sym =&gt; .to_s:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    n.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>x.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">to_sym</span><span style="color:#006600; font-weight:bold;">&#93;</span> = x.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;:sym =&gt; str 1.000.000:&quot;</span>
  t2 = b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Each:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    h.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span> k; v<span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  t2 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;each_key:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    h.<span style="color:#9900CC;">each_key</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k<span style="color:#006600; font-weight:bold;">|</span> k<span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  t2 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;each_value:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    h.<span style="color:#9900CC;">each_value</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>v<span style="color:#006600; font-weight:bold;">|</span> v<span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  t2 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;read by key:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    h.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>k<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  h1=h
&nbsp;
  t3 = b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;5 invert:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    5.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> h.<span style="color:#9900CC;">invert</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  h=h1
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;1000 shift:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    1000.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> h.<span style="color:#9900CC;">shift</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  h=h1
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;1000 delete:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    1000.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> h.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  h=h1
  t3 <span style="color:#006600; font-weight:bold;">+</span>= b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;all delete:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    h.<span style="color:#9900CC;">each_key</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k<span style="color:#006600; font-weight:bold;">|</span> h.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span>k<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#006600; font-weight:bold;">&#91;</span>t1<span style="color:#006600; font-weight:bold;">+</span>t2<span style="color:#006600; font-weight:bold;">+</span>t3<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p><strong>Ruby 1.8.6</strong></p>
<pre>
C:ProgettiRubybench>ruby bench_hsh.rb
                           user     system      total        real
Write 1.000.000:
int => str:            0.851000   0.030000   0.881000 (  0.881320)
int => int:            1.021000   0.090000   1.111000 (  1.141710)
int => [int]:          0.791000   0.030000   0.821000 (  0.841260)
.to_sym => .to_s:      6.400000   0.080000   6.480000 (  6.509750)
:sym => str 1.000.000:
Each:                  2.553000   0.090000   2.643000 (  2.684020)
each_key:              0.481000   0.000000   0.481000 (  0.490735)
each_value:            0.491000   0.000000   0.491000 (  0.490735)
read by key:           2.854000   0.040000   2.894000 (  2.904350)
5 invert:             31.185000   0.791000  31.976000 ( 32.438585)
1000 shift:           37.604000   0.010000  37.614000 ( 38.177180)
1000 delete:           0.000000   0.000000   0.000000 (  0.000000)
all delete:            1.442000   0.010000   1.452000 (  1.452175)
Total:                85.673000   1.171000  86.844000 ( 88.011820)
</pre>
<p><strong>Ruby 1.9.1</strong></p>
<pre>
C:ProgettiRubybench>ruby bench_hsh.rb
                           user     system      total        real
Write 1.000.000:
int => str:            0.721000   0.100000   0.821000 (  0.841260)
int => int:            0.590000   0.140000   0.730000 (  0.731095)
int => [int]:          0.391000   0.020000   0.411000 (  0.440660)
.to_sym => .to_s:      4.957000   0.320000   5.277000 (  5.327980)
:sym => str 1.000.000:
Each:                  0.691000   0.050000   0.741000 (  0.801200)
each_key:              0.461000   0.000000   0.461000 (  0.470705)
each_value:            0.500000   0.000000   0.500000 (  0.520780)
read by key:           1.022000   0.011000   1.033000 (  1.051575)
5 invert:             33.939000   1.071000  35.010000 ( 35.603325)
1000 shift:           48.800000   0.020000  48.820000 ( 49.474100)
1000 delete:           0.000000   0.000000   0.000000 (  0.000000)
all delete:            1.432000   0.010000   1.442000 (  1.472205)
Total:                93.504000   1.742000  95.246000 ( 96.734885)
</pre>
<p><strong>JRuby</strong></p>
<pre>
C:ProgettiRubybench>jruby bench_hsh.rb
                           user     system      total        real
Write 1.000.000:
int => str:            2.113000   0.000000   2.113000 (  1.933000)
int => int:            0.721000   0.000000   0.721000 (  0.721000)
int => [int]:          1.022000   0.000000   1.022000 (  1.022000)
.to_sym => .to_s:     34.641000   0.000000  34.641000 ( 34.641000)
:sym => str 1.000.000:
Each:                  0.541000   0.000000   0.541000 (  0.541000)
each_key:              0.191000   0.000000   0.191000 (  0.191000)
each_value:            0.240000   0.000000   0.240000 (  0.240000)
read by key:           0.761000   0.000000   0.761000 (  0.761000)
5 invert:            Error: Your application used more memory than the safety cap.
</pre>
<p><strong>IronRuby</strong></p>
<pre>
C:ProgettiRubybench>ir bench_hsh.rb
                           user     system      total        real
Write 1.000.000:
int => str:            2.183139   0.030043   2.213182 (  2.213315)
int => int:            1.211742   0.030043   1.241786 (  1.291935)
int => [int]:          1.712462   0.010014   1.722477 (  1.732595)
.to_sym => .to_s:      7.460728   0.120173   7.580901 (  7.781655)
:sym => str 1.000.000:
Each:                  1.111598   0.000000   1.111598 (  1.131695)
each_key:              0.340490   0.000000   0.340490 (  0.350525)
each_value:            0.330475   0.010014   0.340490 (  0.350525)
read by key:           1.021469   0.000000   1.021469 (  1.031545)
5 invert:              5.588035   0.350504   5.938539 (  7.381055)
1000 shift:            0.020029   0.000000   0.020029 (  0.190285)
1000 delete:           0.020029   0.000000   0.020029 (  0.080120)
all delete:            1.031483   0.020029   1.051512 (  1.061590)
Total:                22.031680   0.570821  22.602501 ( 24.596839)
</pre>
<p><strong>Riepilogo totali</strong>:<br />
<code><br />
Ruby 1.8.6<br />
Total:                85.673000   1.171000  86.844000 ( 88.011820)<br />
Ruby 1.9.1<br />
Total:                93.504000   1.742000  95.246000 ( 96.734885)<br />
Jruby<br />
*** Out of memory ***<br />
Ironruby<br />
Total:                22.031680   0.570821  22.602501 <strong>( 24.596839)</strong><br />
</code></p>
<p><strong>Commento</strong>: JRuby non ha concluso per un errore di out of memory, il processo java è arrivato ad occupare 600Mb nel test dove si effettua un'inversione degli hash, raggiungendo il limite della JVM. Il meno ingordo di memoria è stato ruby 1.8.6 con "soli" 300Mb mentre la 1.9.1 era quasi fisso su 530Mb, non aumentava, come se ci fosse un muro. Non c'era invece per Ironruby che ha superato la barriera dei 600Mb ma con risultati soprendenti, il test dell'inversione in 5 secondi, contro 33 della 1.9.1!</p>
<h3>Rieseguengo il test con 2Gb di ram:</h3>
<p><strong>Ruby 1.8.6</strong></p>
<pre>
C:ProgettiRubybench>ruby bench_hsh.rb
                           user     system      total        real
Write 1.000.000:
int => str:            0.811000   0.060000   0.871000 (  0.871252)
int => int:            1.081000   0.050000   1.131000 (  1.141642)
int => [int]:          0.751000   0.030000   0.781000 (  0.801152)
.to_sym => .to_s:      6.299000   0.130000   6.429000 (  6.469302)
:sym => str 1.000.000:
Each:                  2.474000   0.060000   2.534000 (  2.553672)
each_key:              0.491000   0.000000   0.491000 (  0.490706)
each_value:            0.500000   0.000000   0.500000 (  0.500720)
read by key:           2.834000   0.080000   2.914000 (  2.964263)
5 invert:             30.454000   0.992000  31.446000 ( 31.885849)
1000 shift:           38.285000   0.020000  38.305000 ( 38.765743)
1000 delete:           0.000000   0.000000   0.000000 (  0.000000)
all delete:            1.472000   0.010000   1.482000 (  1.512174)
Total:                85.452000   1.432000  86.884000 ( 87.956475)
</pre>
<p><strong>Ruby 1.9.1</strong></p>
<pre>
C:ProgettiRubybench>ruby bench_hsh.rb
                           user     system      total        real
Write 1.000.000:
int => str:            0.701000   0.090000   0.791000 (  0.790640)
int => int:            0.661000   0.100000   0.761000 (  0.780631)
int => [int]:          0.360000   0.020000   0.380000 (  0.390316)
.to_sym => .to_s:      4.947000   0.380000   5.327000 (  5.524472)
:sym => str 1.000.000:
Each:                  0.631000   0.041000   0.672000 (  0.690558)
each_key:              0.451000   0.000000   0.451000 (  0.450365)
each_value:            0.490000   0.000000   0.490000 (  0.490397)
read by key:           0.892000   0.030000   0.922000 (  0.940761)
5 invert:             27.760000   6.299000  34.059000 ( 34.547961)
1000 shift:           48.289000   0.020000  48.309000 ( 48.789488)
1000 delete:           0.000000   0.000000   0.000000 (  0.000000)
all delete:            1.072000   0.370000   1.442000 (  1.441166)
Total:                86.254000   7.350000  93.604000 ( 94.836755)
</pre>
<p><strong>JRuby</strong></p>
<pre>
C:ProgettiRubybench>jruby bench_hsh.rb
                           user     system      total        real
Write 1.000.000:
int => str:            2.261000   0.000000   2.261000 (  2.081000)
int => int:            0.760000   0.000000   0.760000 (  0.760000)
int => [int]:          1.051000   0.000000   1.051000 (  1.051000)
.to_sym => .to_s:     35.694000   0.000000  35.694000 ( 35.694000)
:sym => str 1.000.000:
Each:                  0.481000   0.000000   0.481000 (  0.481000)
each_key:              0.190000   0.000000   0.190000 (  0.190000)
each_value:            0.190000   0.000000   0.190000 (  0.190000)
read by key:           0.701000   0.000000   0.701000 (  0.701000)
5 invert:            Error: Your application used more memory than the safety cap.

C:ProgettiRubybench>jruby bench_hsh.rb -J-Xmx1500m
                           user     system      total        real
Write 1.000.000:
int => str:            1.612000   0.000000   1.612000 (  1.562000)
int => int:            0.842000   0.000000   0.842000 (  0.842000)
int => [int]:          1.101000   0.000000   1.101000 (  1.101000)
.to_sym => .to_s:     35.341000   0.000000  35.341000 ( 35.341000)
:sym => str 1.000.000:
Each:                  0.431000   0.000000   0.431000 (  0.431000)
each_key:              0.180000   0.000000   0.180000 (  0.180000)
each_value:            0.190000   0.000000   0.190000 (  0.200000)
read by key:           0.701000   0.000000   0.701000 (  0.691000)
5 invert:             11.076000   0.000000  11.076000 ( 11.076000)
1000 shift:            0.000000   0.000000   0.000000 (  0.000000)
1000 delete:           0.000000   0.000000   0.000000 (  0.000000)
all delete:            0.912000   0.000000   0.912000 (  0.912000)
Total:                52.385999   0.000000  52.385999 ( 52.335999)
</pre>
<p><strong>IronRuby</strong></p>
<pre>
C:ProgettiRubybench>ir bench_hsh.rb
                           user     system      total        real
Write 1.000.000:
int => str:            2.283283   0.090130   2.373413 (  2.363398)
int => int:            1.201728   0.030043   1.231771 (  1.241786)
int => [int]:          1.792578   0.020029   1.812606 (  1.862678)
.to_sym => .to_s:      7.340555   0.120173   7.460728 (  7.590915)
:sym => str 1.000.000:
Each:                  1.091570   0.000000   1.091570 (  1.141642)
each_key:              0.310446   0.000000   0.310446 (  0.310447)
each_value:            0.360518   0.000000   0.360518 (  0.360518)
read by key:           1.001440   0.000000   1.001440 (  1.001440)
5 invert:              4.816926   0.070101   4.887027 (  4.917070)
1000 shift:            0.010014   0.000000   0.010014 (  0.010014)
1000 delete:           0.010014   0.000000   0.010014 (  0.010015)
all delete:            1.061526   0.000000   1.061526 (  1.071541)
Total:                21.280600   0.330475  21.611075 ( 21.881464)
</pre>
<p><strong>Riepilogo totali</strong>:<br />
<code><br />
Ruby 1.8.6<br />
Total:                85.452000   1.432000  86.884000 ( 87.956475)<br />
Ruby 1.9.1<br />
Total:                86.254000   7.350000  93.604000 ( 94.836755)<br />
Jruby<br />
Total:                52.385999   0.000000  52.385999 ( 52.335999)<br />
Ironruby<br />
Total:                21.280600   0.330475  21.611075 <strong>( 21.881464)</strong><br />
</code></p>
<p><strong>Commento</strong>: JRuby si blocca al solito test e sempre con un processo di 600Mb, ho quindi alzato il cap di memoria a 1,5Gb e gli ha permesso di concludere ed anche con un buon tempo ma il processo ha raggiunto dimensioni mostruose, ha di poco superato il giga. Ruby 1.8.6 sempre il meno esoso con 300Mb e la 1.9.1 sempre 530Mb. Entrambi hanno tempi alti con l'inversione ed in particolare con il metodo shift al contrario degli altri due. Ironruby ancora il più veloce con un processo che si accorge della maggiore quantità di memoria e ne usa un altro pò arrivando ad allocare quasi 700Mb.<br />
Le delete degli elementi è sempre stata velocissima anche se aumentavo il numero, forse non ho controllato bene che non venisse effettuata in un unica chiamata, comunque mi accontento di questo risultato.</p>
<h3>Conclusioni:</h3>
<p>Ogni interprete ha i propri ambiti dove eccelle, Ironruby quando lo fa, lo fa bene distanziando gli altri. Anche JRuby si è preso i suoi trionfi, forse avevo un pò più aspettative dalla versione 1.9.1 anche se credo non sia un suo demerito bensì sono stati gli altri a migliorare molto negli ultimi mesi. La versione 1.8.6 mi ha ben impressionato in questa sua nuova versione o meglio patch 368, è migliorata molto accorciando le distanze prestazionali con la nuova 1.9, pur non avendo una VM. Inoltre è la versione più equilibrata senza alti nè bassi e c'è da considerare che è ancora la versione più usata, più compatibile e con il maggior numero di gemme.</p>
<p>Spero che questo articolo sia risultato interessante, nel prossimo includerò un confronto con python 2.6.2 e la nuova versione 3.1.1 che mi risulta essere velocissima, hola!</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/08/quattro-interpreti-ruby-a-confronto/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>FreeTTS, una libreria java in jRuby on Rails</title>
		<link>http://mastrodonato.info/index.php/2009/08/freetts-una-libreria-java-in-jruby-on-rails/</link>
		<comments>http://mastrodonato.info/index.php/2009/08/freetts-una-libreria-java-in-jruby-on-rails/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 15:08:19 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Ruby on Rails]]></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=75</guid>
		<description><![CDATA[In questo articolo vedremo come utilizzare le classi java contenute in un file JAR
Per questo scopo andremo ad utilizzare una simpatica libreria opensource sviluppata dalla Carnegie Mellon University: FreeTTS.
L&#8217;acronimo TTS significa Text To speech tradotto: &#8220;da testo a voce&#8221;, permette infatti di trasformare un testo in formato audio. Noi la utilizzeremo in un progetto jRuby [...]]]></description>
			<content:encoded><![CDATA[<p>In questo articolo vedremo come utilizzare le classi java contenute in un file JAR<br />
Per questo scopo andremo ad utilizzare una simpatica libreria opensource sviluppata dalla Carnegie Mellon University: <a href="http://freetts.sourceforge.net/">FreeTTS</a>.<br />
L&#8217;acronimo TTS significa Text To speech tradotto: &#8220;da testo a voce&#8221;, permette infatti di trasformare un testo in formato audio. Noi la utilizzeremo in un progetto jRuby on Rails per farci leggere il testo che inseriremo nel db.</p>
<p>Nel <a href="http://mastrodonato.info/index.php/2009/07/configurare-un-ambiente-per-jruby-on-rails/">precedente articolo</a>, abbiamo visto come configurare l&#8217;ambiente, partiamo quindi creando la nuova applicazione:</p>
<p><code><br />
C:>rails ProvaFreeTTS<br />
</code></p>
<p>Nella cartella lib (dalla root) creiamo una sotto cartella freetts, scarichiamo il file <a href="http://sourceforge.net/projects/freetts/files/">freetts-1.2.2-bin.zip</a>, scompattiamo il contenuto in una cartella temporanea, copiamo solamente il contenuto della cartella lib (files jar e jsapi) nella cartella appena creata: tua_applicazionelibfreetts.</p>
<p>Ora creiamo l&#8217;interfaccia per il 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>Creiamo una semplicissima risorsa &#8220;<em>sentence</em>&#8221; con un solo campo &#8220;<em>body</em>&#8221;</p>
<p><code><br />
C:ProvaFreeTTS>jruby script/generate scaffold sentence body:text<br />
</code></p>
<p>Ora creiamo due nuove operazioni, come ho spiegato approfonditamente in un <a href="http://mastrodonato.info/index.php/2009/06/aggiungere-un-operazione-ad-una-risorsa-in-un-sistema-restful/">precedente articolo</a>.</p>
<p>Iniziamo dal controller aggiungendo in coda:</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>Ora andiamo a modificare le viste.<br />
Creiamo un nuovo file, più precisamente un partial dove inseriremo il form dati per la nostra risorsa, in questo modo utilizzeremo lo stesso codice per tutte le operazioni:</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>Aggiungiamo un form clone di quello creato dallo scaffold ma con l&#8217;action diversa, per richiamare l&#8217;operazione speak.</p>
<p>Ora modifichiamo le viste create in automatico:</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>Infine modifichiamo la lista per richiamare la seconda delle operazioni implementate, read. Sostanzialmente cliccando sulla riga corrispondente leggerà il testo precedentemente memorizzato nel db.</p>
<p>Dobbiamo solamente aggiungere una riga ottenendo questa 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>Infine andiamo a modificare il 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;">#sostituiamo map.resources :sentences con</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;">#aggiungiamo</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>Configuriamo il file database.yml, creiamo il db e le tabelle con rake e facciamo partire glassfish come spiegato <a href="http://mastrodonato.info/index.php/2009/07/configurare-un-ambiente-per-jruby-on-rails/">qua</a> (cerca database.yml)</p>
<p>Come abbiamo visto, utilizzare librerie java con jruby on rails è semplicissimo, siamo pronti per far pronunciare al nostro server tutte le frasi più sporcaccione!</p>
<p>Il <strong>progetto completo</strong> potete scaricarlo <a href="http://www.gigasize.com/get.php?d=p4wsyz5z29c">qua</a>.</p>
<p>Per ulteriori approfondimenti c&#8217;è anche questo <a href="http://ruby.html.it/articoli/leggi/3110/jruby-java-e-ruby-insieme/1/">articolo</a>, mentre <a href="http://java.html.it/articoli/leggi/3098/text-to-speech-con-java-e-freetts/1/">questo </a>tratta la libreria FreeTTS in un progetto Java.</p>
<p>Buona continuazione.</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/08/freetts-una-libreria-java-in-jruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Configurare un ambiente per jRuby on Rails</title>
		<link>http://mastrodonato.info/index.php/2009/07/configurare-un-ambiente-per-jruby-on-rails/</link>
		<comments>http://mastrodonato.info/index.php/2009/07/configurare-un-ambiente-per-jruby-on-rails/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 16:52:31 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Glassfish]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[jRuby]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=54</guid>
		<description><![CDATA[Nei prossimi articoli utilizzerò jruby 1.3.1 con Rails 2.3.3 e ne approfitto per scrivere due righe su come configurare il sistema.
A proposito, il sistema utilizzato è un windows xp virtualizzato, java sdk 6, glassfish come application server e javadb.
Scarichiamo e installiamo l&#8217;sdk java, siamo alla versione 6 update 14.
C&#8217;è anche il download dell&#8217;sdk con netbeans, [...]]]></description>
			<content:encoded><![CDATA[<p>Nei prossimi articoli utilizzerò jruby 1.3.1 con Rails 2.3.3 e ne approfitto per scrivere due righe su come configurare il sistema.<br />
A proposito, il sistema utilizzato è un windows xp virtualizzato, java sdk 6, glassfish come application server e javadb.</p>
<p>Scarichiamo e installiamo l&#8217;<a href="http://java.sun.com/javase/downloads/index.jsp" target="_blank">sdk java</a>, siamo alla versione 6 update 14.</p>
<p>C&#8217;è anche il download dell&#8217;sdk con netbeans, un ottimo IDE per gestire progetti ruby on rails. Potrebbe essere un occasione per provarlo. Il pacchetto comprende anche l&#8217;application server glassfish V3.</p>
<p>Ora scarichiamo e installiamo <a href="http://www.jruby.org/" target="_blank">jRuby</a>, siamo alla versione 1.3.1.</p>
<p>L&#8217;installazione è semplicissima: scompattiamo lo zip in un percorso del tipo C:\ruby\jruby-131</p>
<p>Andiamo ad impostare le variabili d&#8217;ambiente, tasto destro su &#8220;Risorse del computer&#8221; sul desktop -> &#8220;Proprietà&#8221; -> seleziona il tab &#8220;Avanzate&#8221; -> click su &#8220;variabili d&#8217;ambiente&#8221;.</p>
<ul>
<li>Aggiungiamo alla variabile <strong>PATH</strong> il percorso della cartella bin dove abbiamo installato jruby, per esempio C:\ruby\jruby-131\bin.</li>
<li>Impostiamo la variabile d&#8217;ambiente <strong>JAVA_HOME</strong>.  Nella zona &#8220;variabili di sistema&#8221; -> &#8220;nuovo&#8221; -> come nome &#8220;JAVA_HOME&#8221; come valore il percorso di installazione dell&#8217;sdk per esempio C:\Programmi\Java\jdk1.6.0_14</li>
</ul>
<p>Non è necessario riavviare il sistema operativo, testiamo il risultato:<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! Ora abbiamo jruby, jirb ed il solito gem. <strong>Se nel sistema è presente anche ruby classico è necessario specificare jruby -S</strong> per richiamare gem, utilizziamolo subito per installare 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>Installiamo l&#8217;adapter jdbc per collegarci a java DB (da ora ometto jruby -S, nel mio sistema non è necessario):<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>Poi installiamo la gemma per 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>Siamo pronti, creiamo un nuovo progetto:<br />
<code><br />
C:\>rails ProvaArticolo<br />
</code></p>
<p>&#8230;nel file di configurazione dei db utilizziamo:</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:\ruby\jruby-131\lib</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>Facciamo partire il server, dal prompt dentro la nostra applicazione:<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>All&#8217;indirizzo http://localhost:3000/ dovremmo vedere la pagina di benvenuto.<br />
Ora fermiamo il server con Ctrl+C e creiamo il db in ambiente sviluppo:</p>
<p><code>C:\ProvaArticolo><strong>rake db:create</strong><br />
(in C:/ProvaArticolo)<br />
db/development.db already exists</code></p>
<p>Creiamo qualcosa all&#8217;interno del database, iniziamo con la risorsa di 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>e creiamo la tabella nel db con la migrate:<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>Infine eliminiamo il file index.html da dentro la cartella public e creiamo la route iniziale:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#config\routes.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>Riavviamo il server e sempre all&#8217;indirizzo http://localhost:3000/ questa volta dovremmo vedere la lista degli articoli.</p>
<p>Se non mi sono dimenticato qualcosa, jruby on rails è pronto, buon divertimento!</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/07/configurare-un-ambiente-per-jruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Aggiungere un operazione ad una risorsa in un sistema RESTful</title>
		<link>http://mastrodonato.info/index.php/2009/06/aggiungere-un-operazione-ad-una-risorsa-in-un-sistema-restful/</link>
		<comments>http://mastrodonato.info/index.php/2009/06/aggiungere-un-operazione-ad-una-risorsa-in-un-sistema-restful/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 10:31:00 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[CRUD]]></category>
		<category><![CDATA[RESTful]]></category>
		<category><![CDATA[risorsa]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=31</guid>
		<description><![CDATA[Creare un applicazione RESTful con rails è molto semplice:
C:>rails MyApp
In un sistema REST, l&#8217;applicazione è composta da risorse web aventi un insieme vincolato di operazioni. Con lo scaffold generiamo una risorsa e le quattro basilari funzioni per gestirla, denominate CRUD (Create, Read, Update, Delete)
C:>script/generate scaffold assets name:string
Abbiamo creato la base da cui partire per testare [...]]]></description>
			<content:encoded><![CDATA[<p>Creare un applicazione <a href="http://it.wikipedia.org/wiki/Representational_State_Transfer" target="_blank">RESTful</a> con rails è molto semplice:</p>
<p><code>C:>rails MyApp</code></p>
<p>In un sistema REST, l&#8217;applicazione è composta da risorse web aventi un insieme vincolato di operazioni. Con lo scaffold generiamo una risorsa e le quattro basilari funzioni per gestirla, denominate <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete" target="_blank">CRUD</a> (Create, Read, Update, Delete)</p>
<p><code>C:>script/generate scaffold assets name:string</code></p>
<p>Abbiamo creato la base da cui partire per testare questo breve articolo.</p>
<p>Supponiamo di voler aggiungere una nuova operazione alla nostra risorsa risorsa <em>Asset</em>, per esempio vogliamo gestire la copia. Iniziamo creando il nuovo indirizzamento modificando quanto creato dallo scaffold:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#config/routes.rb</span>
map.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:assets</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;">:copy</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></pre></div></div>

<p>Testiamo l&#8217;esito:<br />
<code>C:>rake routes</code></p>
<p>verificando che ci sia il nuovo percorso:<br />
<code><br />
copy_asset GET   /assets/:id/copy(.:format)   {:controller=>"assets", :action=>"copy"}<br />
</code></p>
<p>La copia è un&#8217;operazione utile in molti contesti in quanto permette di effettuare l&#8217;inserimento di una nuova risorsa partendo da una già esistente.<br />
Traduciamo in codice e andiamo a creare la nuova operazione nel controller:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#app/controllers/assets_controller.rb</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># GET /assets/copy</span>
<span style="color:#008000; font-style:italic;"># GET /assets/copy.xml</span>
<span style="color:#9966CC; font-weight:bold;">def</span> copy
  <span style="color:#0066ff; font-weight:bold;">@asset</span> = asset.<span style="color:#9900CC;">new</span>
  <span style="color:#0066ff; font-weight:bold;">@asset</span>.<span style="color:#9900CC;">attributes</span> = Asset.<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:#9900CC;">attributes</span>
&nbsp;
  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:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:new</span> <span style="color:#006600; font-weight:bold;">&#125;</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:#0066ff; font-weight:bold;">@asset</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>

<p>Eventualmente si può creare la view, dipende dall&#8217;operazione che si deve gestire. In questo esempio si sfrutta la view dell&#8217;operazione new in quanto sono molto simili e richiamano entrambe la :create.</p>
<p>Creare o non creare una nuova azione sulla risorsa è pura filosofia, tralasciando la questione vediamo come indirizzare una nuova create, ci riferiamo ad una collezione di risorse:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#config/routes.rb</span>
map.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:assets</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;">:copy</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;">:createcp</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></pre></div></div>

<p>ho utilizzato una collezione perchè in questo caso non serve un riferimento ad una risorsa esistente, se avessi usato :member il path per la generazione dell&#8217;uri lo avrebbe richiesto.</p>
<p>se testiamo nuovamente le routes dovrebbe aggiungersi questa nuova:<br />
<code><br />
createcp_asset PUT   /assets/createcp(.:format)   {:controller=>"assets", :action=>"createcp"}<br />
</code></p>
<p>creiamo quindi la nuova view e il form con la uri della nuova azione specificando il metodo (per precisione ho indicato put ma equivale ad utilizzare un post) :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">#app/views/assets/copy.html.erb
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span> form_for<span style="color:#006600; font-weight:bold;">&#40;</span>@asset, <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> createcp_assets_path, <span style="color:#ff3333; font-weight:bold;">:method</span><span style="color:#006600; font-weight:bold;">=&gt;</span>:put <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>
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span><span style="color:#006600; font-weight:bold;">&#41;</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_field</span> <span style="color:#ff3333; font-weight:bold;">:name</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> t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:create</span><span style="color:#006600; font-weight:bold;">&#41;</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>e la nuova azione nel controller:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#app/controllers/assets_controller.rb</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># POST /assets/createcp</span>
<span style="color:#008000; font-style:italic;"># POST /assets/createcp.xml</span>
<span style="color:#9966CC; font-weight:bold;">def</span> createcp
  <span style="color:#0066ff; font-weight:bold;">@asset</span> = Asset.<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;">:asset</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;">#do something</span>
&nbsp;
  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:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@asset</span>.<span style="color:#9900CC;">save</span>
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</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;">:created</span>, <span style="color:#ff3333; font-weight:bold;">:model</span> <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:#996600;">'model.asset'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> redirect_to assets_path <span style="color:#006600; font-weight:bold;">&#125;</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:#0066ff; font-weight:bold;">@asset</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;">:created</span>, <span style="color:#ff3333; font-weight:bold;">:location</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@asset</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;copy&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</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:#0066ff; font-weight:bold;">@asset</span>.<span style="color:#9900CC;">errors</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>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Per richiamare la nuova operazione dalla index:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">#app/views/assets/index.html.erb
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:copy</span><span style="color:#006600; font-weight:bold;">&#41;</span>, copy_asset_path<span style="color:#006600; font-weight:bold;">&#40;</span>asset<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Infine ed eventualmente, queste sono le parole utilizzate dall&#8217;esempio per l&#8217;internazionalizzazione:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">create: <span style="color:#996600;">&quot;Crea&quot;</span>
copy: <span style="color:#996600;">&quot;Copia&quot;</span>
created: <span style="color:#996600;">&quot;L'oggetto {{model}} è stato creato correttamente.&quot;</span>
name: <span style="color:#996600;">&quot;Nome&quot;</span>
model:
  asset: <span style="color:#996600;">&quot;Allegato&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/06/aggiungere-un-operazione-ad-una-risorsa-in-un-sistema-restful/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
