<?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>Wed, 21 Sep 2011 07:12:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Una nuova casa</title>
		<link>http://mastrodonato.info/index.php/2011/09/una-nuova-casa/</link>
		<comments>http://mastrodonato.info/index.php/2011/09/una-nuova-casa/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 14:14:50 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=712</guid>
		<description><![CDATA[Mi sto spostando su blogger, questo è il link del mio nuovo blog.]]></description>
			<content:encoded><![CDATA[<p>Mi sto spostando su blogger, questo è il link del mio <a href="http://marco.mastrodonato.info/">nuovo blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2011/09/una-nuova-casa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Leonardo: un generatore di applicazioni Rails 3.1</title>
		<link>http://mastrodonato.info/index.php/2011/08/leonardo-generatore-di-applicazioni-rails-3-1/</link>
		<comments>http://mastrodonato.info/index.php/2011/08/leonardo-generatore-di-applicazioni-rails-3-1/#comments</comments>
		<pubDate>Mon, 22 Aug 2011 21:54:42 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Generatori]]></category>
		<category><![CDATA[Leonardo]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=697</guid>
		<description><![CDATA[L&#8217;aggiornamento alla versione più recente lo trovi sul mio nuovo blog. Click here for the english version. Ho creato un nuovo generatore per creare applicazioni rails 3.1 (che attualmente si trova in rc6) ed automatizzare le operazioni che risultano ripetitive. Il nome della gemma è leonardo ed è suddivisa in due generatori: leolay: per creare [...]]]></description>
			<content:encoded><![CDATA[<p><strong>L&#8217;aggiornamento alla versione più recente lo trovi <a href="http://marco.mastrodonato.info/2011/09/leonardo-un-generatore-di-applicazioni.html">sul mio nuovo blog</a>. Click <a href="http://en-marcomastrodonato.blogspot.com/2011/09/leonardo-rails-31-application-generator.html">here for the english version</a>.</strong></p>
<p>Ho creato un nuovo generatore per creare applicazioni rails 3.1 (che attualmente si trova in rc6) ed automatizzare le operazioni che risultano ripetitive.</p>
<p>Il nome della gemma è <a href="https://rubygems.org/gems/leonardo">leonardo</a> ed è suddivisa in due generatori:</p>
<ol>
<li><strong>leolay</strong>: per creare il layout</li>
<li><strong>leosca</strong>: per creare la risorsa (sostituisce lo scaffold)</li>
</ol>
<h2>Perchè creare un nuovo generatore se esiste già lo scaffold di rails?</h2>
<p>Lo scaffold è un comodo generatore e si può anche personalizzare con facilità ma è adatto per scopi didattici e la personalizzazione è limitata alle sole viste. In produzione mi serviva qualcosa di più completo e personalizzabile.</p>
<h2>Preparazione dell&#8217;ambiente ed installazione della gemma</h2>
<p>Creiamo un nuovo ambiente ruby ed installiamo rails 3.1:<br />
gem install rails &#8211;pre<br />
usiamo il &#8211;pre perchè in questo momento si trova in release candidate, toglietelo se invece è stata rilasciata</p>
<p>Possiamo installare la gemma semplicemente con:<br />
gem install leonardo</p>
<p>&#8230;ma non è necessario e consiglio di usare il procedimento che sto per descrivere:<br />
creiamo una nuova applicazione utilizzando il template che ho preparato, raccomandato per agevolare l&#8217;avvio e per meglio sfruttare la gemma:</p>
<p>rails new NewApp -m http://cloud.github.com/downloads/marcomd/Leonardo/template.rb</p>
<p>Se non dovesse funzionare il link potete comunque trovare il template nella root della gemma</p>
<p>Il template chiederà se installare determinate gemme esterne, per una prova consiglio di installarle tutte rispondendo <strong>y</strong> o premendo invio quando propone un default (es. devise)</p>
<p>Verrà poi eseguito un bundle install per verificare la presenza delle gemme ed in seguito verranno eseguite le varie generazioni, il tutto durerà qualche minuto circa.<br />
La generazione del layout chiederà una conferma per sostituire il file en.yml, rispondete <strong>y</strong>. Preferisco non forzare la sostituzione per permettere altre ed eventuali esecuzioni del generatore leolay nel caso si volesse aggiornare il layout.</p>
<p>Al termine otterrete un applicazione &#8220;pronta per partire&#8221; potendo così orientare lo sviluppo sulla parte applicativa.</p>
<p>Spostiamoci nella cartella dell&#8217;applicazione ed avviamo il server<br />
cd NewApp<br />
rails s<br />
indirizziamo il browser all&#8217;indirizzo http://localhost:3000 per accedere alla home</p>
<p>Se clicchiamo su &#8220;<em>Sign in</em>&#8221; verrà richiesta l&#8217;autenticazione (se l&#8217;avete inclusa), per un rapido accesso potete inserire:<br />
email: admin@newapp.com<br />
password: abcd1234</p>
<p><img title="Signed in" src="http://i.imgur.com/BFi37.png" alt="Signed in" /></p>
<p>Vengono create tre utenze con tre ruoli diversi (nel caso avete incluso le autorizzazioni):</p>
<ol>
<li>admin@newapp.com</li>
<li>manager@newapp.com</li>
<li>user@newapp.com</li>
</ol>
<p>Naturalmente si tratta di utenze con finalità legate allo sviluppo per cui ricordate di eliminarle prima di qualsiasi rilascio. Possiamo consultare e modificare i ruoli accedendo al file app/models/ability.rb in quanto la gestione è affidata alla gemma <a href="https://github.com/ryanb/cancan">cancan</a>.</p>
<p>Per cambiare la lingua è sufficiente inviare la nuova come valore del parametro lang, esempio:</p>
<p>http://localhost:3000/?lang=it</p>
<p>il generatore attualmente gestisce :en e :it ma aggiungere il supporto ad un&#8217;altra lingua è semplice, basta aggiungere il file yml nella cartella dove si trovano i primi due, naturalmente deve avere gli stessi tags.</p>
<h2>Generare le risorse</h2>
<p>Ora generiamo qualche risorsa utilizzando il generatore leosca, una sorta di scaffold personalizzato:</p>
<p>rails g leosca category name:string active:boolean</p>
<p>rails g leosca product category:references name:string description:text price:decimal</p>
<p>ora creiamo le due nuove tabelle anche sul database:<br />
rake db:migrate</p>
<p>ed eseguiamo anche il popolamento con qualche dato che leosca ha preparato per noi:<br />
rake db:seed</p>
<p><img title="Listing Categories" src="http://i.imgur.com/rJzCB.png" alt="Listing Categories" /></p>
<p>Alcune note:</p>
<ul>
<li>L&#8217;esportazione dei dati in csv attualmente non funziona se attiva la paginazione ajax, sto cercando un buon metodo per risolvere.</li>
<li>Nelle liste vengono inseriti in automatico tutti i campi per poter applicare dei filtri, eliminiamo quelli che non ci interessano. I filtri su campi boolean non funzionano con sqlite3 a causa di un bug nel driver che non genera un sql corretto. Con SQLServer funziona alla perfezione.</li>
<li>Gli oggetti relazionati mostrano un link col nome e cliccando si effettua una :show. Nel caso la tabella non abbia il campo :name viene utilizzato l&#8217;id che è possibile sostituire con il dato che più lo rappresenta.</li>
<li>Solo l&#8217;operazione destroy è gestita tramite ajax. Show, edit e create attualmente sono gestite tramite http.</li>
<li>Riguardo le utenze create in automatico: con i ruoli di default admin può fare tutto, manager tutte le operazioni crud, user tutte tranne la delete.</li>
<li>Le etichette dei campi vengono inserite in automatico ma la traduzione è necessario farsela da se all&#8217;interno dei files config/locales/*.yml</li>
</ul>
<h2>Personalizzare il generatore</h2>
<p>E&#8217; sufficiente installarlo nel progetto e personalizzarlo secondo le proprie esigenze:<br />
rails g leosca:install<br />
si trova sotto lib ed eventuali modifiche avranno la precedenza sulla gemma</p>
<h2>Conclusioni</h2>
<p>Per ulteriori approfondimenti o aggiornamenti sugli sviluppi futuri è possibile consultare la homepage del progetto:<br />
<a href="https://github.com/marcomd/Leonardo">https://github.com/marcomd/Leonardo</a></p>
<p>Altri riferimenti:<br />
<a href="http://edgeguides.rubyonrails.org/generators.html">http://edgeguides.rubyonrails.org/generators.html</a><br />
<a href="http://railscasts.com/episodes/216-generators-in-rails-3">http://railscasts.com/episodes/216-generators-in-rails-3</a><br />
<a href="http://railscasts.com/episodes/218-making-generators-in-rails-3">http://railscasts.com/episodes/218-making-generators-in-rails-3</a><br />
<a href="http://railscasts.com/episodes/242-thor">http://railscasts.com/episodes/242-thor</a><br />
<a href="http://rdoc.info/github/wycats/thor/master/Thor">http://rdoc.info/github/wycats/thor/master/Thor</a><br />
<a href="http://textmate.rubyforge.org/thor/Thor/Actions.html">http://textmate.rubyforge.org/thor/Thor/Actions.html</a><br />
<a href="http://railscasts.com/episodes/265-rails-3-1-overview">http://railscasts.com/episodes/265-rails-3-1-overview</a></p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2011/08/leonardo-generatore-di-applicazioni-rails-3-1/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>HTC Desire HD</title>
		<link>http://mastrodonato.info/index.php/2011/05/htc-desire-hd/</link>
		<comments>http://mastrodonato.info/index.php/2011/05/htc-desire-hd/#comments</comments>
		<pubDate>Mon, 30 May 2011 14:40:44 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Smartphones]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Desire HD]]></category>
		<category><![CDATA[HTC]]></category>
		<category><![CDATA[Smartphone]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=682</guid>
		<description><![CDATA[Uscito a ottobre del 2010, il Desire HD è l&#8217;attuale modello di punta di HTC, almeno fino al prossimo mese, data in cui è prevista l&#8217;uscita del nuovo modello con cpu dual core. Esce con Android Froyo ma è disponibile l&#8217;aggiornamento ad Android 2.3 Gingerbread. Quello che posso aggiungere alle specifiche fornite dalla casa è [...]]]></description>
			<content:encoded><![CDATA[<p>Uscito a ottobre del 2010, il Desire HD è l&#8217;attuale modello di punta di HTC, almeno fino al prossimo mese, data in cui è prevista l&#8217;uscita del nuovo modello con cpu dual core. Esce con Android Froyo ma è disponibile l&#8217;aggiornamento ad Android 2.3 Gingerbread.</p>
<p><img src="http://www.htc.com/uploadedImages/WWW/Product/HTC_Desire_HD/size.jpg" alt="Desire HD" style="float: right; border: none;" /></p>
<p>Quello che posso aggiungere alle <a href="http://www.htc.com/it/specification.aspx?p_id=324">specifiche fornite dalla casa</a> è che utilizza un display in vetro che non ha nulla da invidiare ad un gorilla glass, molto resistente ai graffi.</p>
<p>Dispone della seconda generazione del processore Qualcomm snapdragon. E&#8217; prodotto con tecnologia a 45nm che significa minor consumi e migliore  autonomia, nonostante la batteria di soli 1230mha ed il generoso display da 4,3&#8243; riesce ad arrivare fino a sera. La parte grafica è affidata alla GPU Adreno 205, soluzione progettata e costruita internamente da Qualcomm dopo che ha comprato la divisione &#8220;ATI mobile&#8221; da AMD. La cpu si occupa anche di altri compiti che solitamente sono gestiti da moduli esterni come la ricezione gps. Questo semplifica la progettazione dei terminali con impatti positivi sui costi, motivo che ha permesso a Qualcomm di diffondere agevolmente la propria soluzione. Questi sono un pò di risultati prestazionali subito dopo averlo scartato dalla confezione:</p>
<p>Rom: Stock 1.72.405.3 (Android 2.2.1)<br />
Kernel: 2.6.32.21 del 01/12/2010<br />
CPU: 245-1024Mhz (no undervolt, no oc)<br />
Quadrant: 1700<br />
Linepack: 40,4 MFLOP<br />
Neocore: 58,3 FPS<br />
Nenamark1: 34,3 FPS<br />
Nenamark2: 11,5 FPS</p>
<p>Sul mercato esistono diverse revisioni: inizialmente la sd da 8gb era di classe 2 della samsung, gli ultimi modelli dispongono di una più veloce classe 4 prodotta da Sandisk e di un display migliore con tecnologia Super LCD. Per capire quale dei due display monta si potrebbero esaminare i sub pixel come spiegato <a href="http://pocketnow.com/android/htc-desire-hd-lcd-vs-htc-incredible-s-s-lcd-magnified">qua</a> o questo più semplice metodo empirico: provarlo sotto la luce del sole, se monta un slcd dovrebbe avere una buona o perlomeno discreta visualizzazione delle immagini.</p>
<p>E&#8217; un telefono molto curato, dalla confezione al software che ti accoglie con benvenuti vari, tutorial, ecc. La qualità generale è molto alta e i materiali sono di ottima fattura, dalla scocca monoblocco in alluminio priva di scricchiolii alla gomma usata negli auricolari. A proposito degli auricolari, sono costruiti bene ma purtroppo non significa che suonano altrettanto bene, peccano infatti sulle frequenze medie e basse. Stesso giudizio per l&#8217;altoparlante, ha un suono nitido e cristallino chè reputo ottimo per le suonerie polifoniche ma inadatto per gli mp3. Sinceramente mi sarei aspettato di meglio da un terminale venduto come ottima soluzione per contenuti multimediali.</p>
<p><strong>Confezione:</strong><br />
<a href="http://imgur.com/bNobx"><img src="http://i.imgur.com/bNobxl.jpg" alt="" /></a></p>
<p><strong>Cuffie:</strong><br />
<a href="http://imgur.com/xHyS0"><img src="http://i.imgur.com/xHyS0.jpg" alt="" /></a></p>
<p><strong>Software:</strong><br />
A differenza di altri concorrenti che ho potuto provare come l&#8217;LG Optimus dual od il Galaxy S, l&#8217;interfaccia mi sembra più ottimizzata che si traduce in un utilizzo generale più piacevole, più fluido, più animazioni: di default lo sfondo è animato, quando si accede si viene accolti da un&#8217;appariscente animazione meteo ecc. I widget forniti da htc che si vanno aggiungere a quelli di android, sono tanti, fatti molto bene ed a cui se ne possono aggiungere degli altri scaricandoli dal servizio fornito da htc e vale anche per il set di suoni, sfondi ecc. Registrandosi su htcsense.com è anche possibile monitorare da web gli spostamenti del proprio telefono, utile in caso di smarrimento, un pò meno forse in quelli di furto. </p>
<p><strong>Fotocamera:</strong><br />
Simpatica ed utile la possibilità di applicare effetti di correzione alle foto, è anche possibile vederli applicati mentre si riprende la scena!</p>
<p>Esempio:<br />
<a href="http://imgur.com/ItVxF"><img src="http://i.imgur.com/ItVxFl.jpg" alt="" /></a></p>
<p>Esempio con effetto miglioramento:<br />
<a href="http://i.imgur.com/uryzL"><img src="http://i.imgur.com/uryzLl.jpg" alt="" /></a></p>
<p>E&#8217; sempre meglio non aver troppe aspettative da un cellulare o smartphone che sia. Il rumore nelle foto è spesso presente ed aumenta col diminuire della luce. Di notte la situazione è critica, il doppio flash aiuta molto, sia per le foto che per i video ma a volte non è sufficiente anche per il corto raggio di azione e se si osservano le foto alla loro dimensione originale si capirà subito il motivo.<br />
Il problema non è solo il rumore ma anche ogni piccolo dettaglio risulta come impastato. Tuttavia si possono sfruttare gli 8 megapixel: ridimensionando la fotografia ad una risoluzione inferiore si andranno a mescolare i difetti ottenendo una foto più piccola (ad esempio 5megapixel) ma migliore di quella che normalmente otterremmo con un sensore di quella risoluzione.</p>
<p><a href="http://imgur.com/hMKuY"><img src="http://i.imgur.com/hMKuYl.jpg" alt="" /></a></p>
<p>Questo è un video girato da me a 480p e 3mbit di bit rate:</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/_oITUGjsYYo?hl=it&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/_oITUGjsYYo?hl=it&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p><a href="http://www.youtube.com/results?search_query=desire+hd+720p&#038;aq=f">Qua</a> si possono trovare degli esempi girati a 720p.</p>
<p><strong><br />
Permessi di root:</strong><br />
Per chi non lo sapesse, è possibile ottenere i permessi di root per avere il pieno controllo del terminale. In questo modo è possibile scegliere una versione alternativa del sistema operativo per migliorare le prestazioni, per sistemare bachi o carenze in determinati ambiti ad esempio migliorando l&#8217;autonomia semplicemente sfruttando meglio la batteria. E&#8217; possibile inoltre aggiornare la sezione software che gestisce la ricetrasmissione radio (3g, wifi ecc.) per migliorare anche queste caratteristiche ma devo anche segnalare che questa operazione non è completamente esente da rischi.</p>
<p>Considerando che con questi aggiornamenti non ufficiali si perde la garanzia, io consiglio innanzitutto di usarlo come esce dal negozio per un periodo necessario alla verifica di ogni componente. Dopodichè si deve valutare: se il telefono va bene così com&#8217;è e\o non si ha la voglia di perdere molto tempo per star dietro allo sviluppo delle rom alternative io consiglio di limitarsi agli aggiornamenti ufficiali disponibili fino a gingerbread. Lo smanettone invece non dovrebbe pensarci due volte, sarebbe un peccato non sfruttare le enormi potenzialità delle rom modificate.</p>
<p><strong>Pro e contro delle custom rom:</strong></p>
<p>I vantaggi sono: </p>
<ul>
<li>ottimizzazione software sia del kernel che della rom: migliorando il cuore del sistema si possono avere molti vantaggi: prestazioni, autonomia ecc. potrei riassumere che il telefono risulterà più reattivo e privo di fronzoli</li>
<li>ottimizzazione della cpu: è possibile abbassare il voltaggio migliorando ulteriormente i consumi in quanto quello di default è oltre il valore necessario. E&#8217; anche possibile aumentare la frequenza di funzionamento massima che si traduce in potenza a disposizione quando serve pur mantenendo i consumi ridotti in standby.</li>
<li>backup delle singole applicazioni o dell&#8217;intero telefono</li>
</ul>
<p>&nbsp;<br />
Gli svantaggi:</p>
<ul>
<li>richiede una discreta dimestichezza informatica generale</li>
<li>tempo per seguire gli sviluppi software e scegliere la miglior soluzione per le proprie esigenze</li>
<li>presenza di bug che possono inibire l&#8217;utilizzo di alcune caratteristiche del desire hd anche se basterebbe scegliere solo release collaudate</li>
</ul>
<p>Per saperne di più, posso proporre questa <a href="http://www.androidiani.com/forum/modding-htc-desirehd/48379-come-fare-tutto-raccolta-di-tutti-i-link.html">raccolta di links</a> fornito gentilmente da un forum di appassionati italiani.</p>
<p>Il consiglio che vale sempre per tutto è fare le cose per gradi: iniziare dalla versione stock => testarla per un periodo necessario ad esplorarla a fondo => provare una custom rom stabile => testarla per un periodo necessario ad esplorarla a fondo => passare eventualmente a qualcos&#8217;altro e così via.</p>
<p>Per rimanere aggiornati sulle custom rom è possibile visitare la sezione del <a href="http://forum.xda-developers.com/forumdisplay.php?f=765&#038;order=desc">forum xda dedicata al Desire HD</a> (in inglese).</p>
<p>Appena riesco farò dei confronti con iPhone4, Optimus Dual (2x), Optimus 7, Nokia N8 ed altri modelli che gentilmente mi metteranno a disposizione (amici e parenti).</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2011/05/htc-desire-hd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Files di grandi dimensioni su Windows Server 2008 R2 64Bit</title>
		<link>http://mastrodonato.info/index.php/2011/04/files-grandi-dimensioni-windows-server-2008-r2-64bit/</link>
		<comments>http://mastrodonato.info/index.php/2011/04/files-grandi-dimensioni-windows-server-2008-r2-64bit/#comments</comments>
		<pubDate>Thu, 21 Apr 2011 14:57:57 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[IronRuby .NET]]></category>
		<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Prestazioni]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Benchmark]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Files]]></category>
		<category><![CDATA[IronRuby]]></category>
		<category><![CDATA[jRuby]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=665</guid>
		<description><![CDATA[Nel precedente articolo ho esaminato la semplice lettura e scrittura di tre files con dimensioni da 330Mb a 2,6Gb, su un normalissimo PC con Windows XP. Ora vediamo cosa cambia su un server virtuale, sistema produttivo che coinvolge un numero di aziende sempre più ampio. I linguaggi in esame sono i seguenti: Ruby 1.8.6 p383 [...]]]></description>
			<content:encoded><![CDATA[<p>Nel <a href="http://mastrodonato.info/index.php/2011/04/ruby-python-php-lua-comparazione-files-grandi-dimensioni/">precedente articolo</a> ho esaminato la semplice lettura e scrittura di tre files con dimensioni da 330Mb a 2,6Gb, su un normalissimo PC con Windows XP. Ora vediamo cosa cambia su un server virtuale, sistema produttivo che coinvolge un numero di aziende sempre più ampio.</p>
<p>I linguaggi in esame sono i seguenti:</p>
<p>Ruby 1.8.6 p383 (2009-08-04) [i386-mingw32]<br />
Ruby 1.8.7 p334 (2011-02-18) [i386-mingw32]<br />
Ruby 1.9.2 p180 (2011-02-18) [i386-mingw32]<br />
jruby 1.6.1 (ruby-1.8.7-p330) (2011-04-12) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_23) [Windows Server 2008 R2-amd64-java]<br />
IronRuby 1.1.3.0 (ruby-1.9.2) on .NET 4.0.30319.225<br />
Python 2.7.1 32bit<br />
Python 2.7.1 64bit<br />
Python 3.2.0 32bit<br />
Python 3.2.0 64bit<br />
Php 5.3.6 vc9 unsafe thread<br />
Lua 5.1.4 40<br />
C# 32bit on .NET 2.0.50727.4927<br />
C# 64bit on .NET 2.0.50727.4927<br />
C# 32bit on .NET 4.0.30319.1<br />
C# 64bit on .NET 4.0.30319.1</p>
<p>Solo python fornisce pacchetti di installazione x64 e ne ho approfittato per confrontarli con le versioni a 32 bit. Probabilmente le differenze si noteranno con operazioni matematiche rispetto la sezione IO ma questo apre la strada alla prossima comparazione.</p>
<p>La versione di ruby 1.8.6 è una mingw32 e non mswin32 come nel precedente test. <a href="http://www.ironruby.net/">IronRuby</a> invece è l&#8217;ultima 1.1.3 che abbraccia il supporto a ruby 1.9.2 e non 1.8.6 come la versione del precedente test con cui, comunque, condivide lo stesso framework .net e la stessa sezione IO.<br />
Questa volta ho anche aggiunto C# nella comparazione, ho compilato quattro versioni differenziando per piattaforma, x86 e x64, ed anche per framework, 3.5 e 4. Il framework .net 3.5 utilizza lo stesso <a href="http://it.wikipedia.org/wiki/Common_Language_Runtime">CLR</a> del 2.0. </p>
<p>Una nota di merito a IronRuby, il primo della classe che è persino davanti a C#, linguaggio compilato e con cui condivide molto. E&#8217; vero che questo test non richiede potenza computazionale particolarmente elevata ma è certamente un risultato curioso.</p>
<p>Un riepilogo anche sul consumo della memoria:</p>
<table>
<tr>
<td>Lua 5.1.4</td>
<td><b>0,7mb</b></td>
</tr>
<tr>
<td>Php 5.3.6</td>
<td>2,2mb</td>
</tr>
<tr>
<td>Python 2.7.1 32bit</td>
<td>2,5mb</td>
</tr>
<tr>
<td>Python 3.2.0 32bit</td>
<td>3,7mb</td>
</tr>
<tr>
<td>Python 2.7.1 64bit</td>
<td>4mb</td>
</tr>
<tr>
<td>Python 3.2.0 64bit</td>
<td>5,5mb</td>
</tr>
<tr>
<td>Ruby 1.9.2p180</td>
<td>4-6mb</td>
</tr>
<tr>
<td>Ruby 1.8.6p383</td>
<td>4-9mb</td>
</tr>
<tr>
<td>Ruby 1.8.7p334</td>
<td>4-9mb</td>
</tr>
<tr>
<td>C# 32bit on .NET 2.0.50727.4927</td>
<td>7mb</td>
</tr>
<tr>
<td>C# 32bit on .NET 4.0.30319.1</td>
<td>7mb</td>
</tr>
<tr>
<td>C# 64bit on .NET 2.0.50727.4927</td>
<td>9mb</td>
</tr>
<tr>
<td>C# 64bit on .NET 4.0.30319.1</td>
<td>9mb</td>
</tr>
<tr>
<td>IronRuby 1.1.3.0 on .NET 4.0.30319.225</td>
<td>11mb</td>
</tr>
<tr>
<td>jruby 1.6.1 (JVM 64-Bit Server 1.6.0_23)</td>
<td>jruby 1mb + java 200mb</td>
</tr>
</table>
<p><img src="http://i.imgur.com/np8SWl.jpg" alt="" title="2,6 Gb" /></p>
<p><img src="http://i.imgur.com/obUkdl.jpg" alt="" title="880 Mb" /></p>
<p><img src="http://i.imgur.com/FTWtFl.jpg" alt="" title="330 Mb" /></p>
<p><img src="http://i.imgur.com/Wv6f2l.jpg" alt="" title="Totale" /></p>
<p>Questo è il codice C# che ho compilato con Visual Studio 2010:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> Split
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">class</span> Program
    <span style="color: #008000;">&#123;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// To split a file into n output files</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;args&quot;&gt;Filename and records number to split&lt;/param&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #6666cc; font-weight: bold;">string</span> strInput <span style="color: #008000;">=</span> args<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">string</span> strOutput <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;out_{0:000}.txt&quot;</span><span style="color: #008000;">;</span>
            Int32 nrec_to_split <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToInt32</span><span style="color: #008000;">&#40;</span>args<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            DateTime t1 <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;C# {1} Started at {0:R}, please wait...&quot;</span>, t1, <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Environment</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Version</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
&nbsp;
            StreamReader sr<span style="color: #008000;">;</span>
            StreamWriter sw <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
            sr <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #008000;">&#40;</span>strInput<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            Int16 nsplit <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            Int64 nrec <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>sr<span style="color: #008000;">.</span><span style="color: #0000FF;">Peek</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>nrec <span style="color: #008000;">%</span> nrec_to_split <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #008000;">++</span>nsplit<span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>sw <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> sw<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    sw <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamWriter<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span>strOutput, nsplit<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
                sw<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>sr<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">++</span>nrec<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Ended at {0:R}, please wait...&quot;</span>, DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Elapsed time {0}&quot;</span>, DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span> <span style="color: #008000;">-</span> t1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2011/04/files-grandi-dimensioni-windows-server-2008-r2-64bit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby, Python, Php e Lua in comparazione con files di grandi dimensioni</title>
		<link>http://mastrodonato.info/index.php/2011/04/ruby-python-php-lua-comparazione-files-grandi-dimensioni/</link>
		<comments>http://mastrodonato.info/index.php/2011/04/ruby-python-php-lua-comparazione-files-grandi-dimensioni/#comments</comments>
		<pubDate>Tue, 19 Apr 2011 08:55:44 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[IronRuby .NET]]></category>
		<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Prestazioni]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Benchmark]]></category>
		<category><![CDATA[IronRuby]]></category>
		<category><![CDATA[jRuby]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=653</guid>
		<description><![CDATA[Vediamo come si comporta la sezione IO di alcuni tra i più popolari linguaggi script. L&#8217;esercizio consiste nel leggere sequenzialmente un grosso file di input e dividerlo in files più piccoli, in termini pratici, splittarlo. I linguaggi in esame sono: Ruby 1.8.6 p287 (2008-08-11) [i386-mswin32] Ruby 1.8.7 p334 (2011-02-18) [i386-mingw32] Ruby 1.9.2 p180 (2011-02-18) [i386-mingw32] [...]]]></description>
			<content:encoded><![CDATA[<p>Vediamo come si comporta la sezione IO di alcuni tra i più popolari linguaggi script. L&#8217;esercizio consiste nel leggere sequenzialmente un grosso file di input e dividerlo in files più piccoli, in termini pratici, splittarlo.</p>
<p>I linguaggi in esame sono:<br />
Ruby 1.8.6 p287 (2008-08-11) [i386-mswin32]<br />
Ruby 1.8.7 p334 (2011-02-18) [i386-mingw32]<br />
Ruby 1.9.2 p180  (2011-02-18) [i386-mingw32]<br />
jruby 1.5.1 (ruby 1.8.7 patch 249) (Java HotSpot(TM) Client VM 1.6.0_14) [x86-java]<br />
jruby 1.5.1 (ruby 1.8.7 patch 249) (Java HotSpot(TM) Client VM 1.6.0_24) [x86-java]<br />
jruby 1.6.1 (ruby-1.8.7-p330) (Java HotSpot(TM) Client VM 1.6.0_24) [Windows XP-x86-java]<br />
IronRuby 1.1.0.0 on .NET 4.0.30319.225<br />
Python 2.6.2<br />
Python 2.7.1<br />
Python 3.2.0<br />
Php 5.3.6 vc9 unsafe thread<br />
Lua 5.1.4 40</p>
<p>Iniziamo col creare i tre files di input necessari per il test:<br />
ruby new.rb input1.txt 185000 1799  => 330Mb<br />
ruby new.rb input2.txt 500000 1799  => 880Mb<br />
ruby new.rb input3.txt 1500000 1799 => 2,6Gb</p>
<p>Il test l&#8217;ho eseguito su un PC con cpu Intel E7300 Core2 Duo 2,66Ghz Ram 3,25Gb con Windows XP Professional 32bit, Hard Disk ST3250310AS Barracuda 7200.10 SATA 3.0Gb/s da 250Gb.</p>
<p>Prossimamente lo eseguirò anche su un Server Windows 2008 R2 64bit su VMWare Xeon X7460 Dual Core a 2,66Ghz e 2Gb di ram con dischi SCSI.</p>
<p>Prima e dopo aver creato i tre files di input ho deframmentato il disco. Se i tempi sono incostanti significa che il disco deve essere deframmentato o c&#8217;è qualcosa che rallenta il sistema come ad esempio l&#8217;antivirus che deve essere disabilitato.</p>
<p>Per ogni file ho eseguito sei elaborazioni e considerando le scarse prestazioni IO del sistema, ho scartato le tre peggiori. Naturalmente, prima di ogni test ho eliminato i files di output. </p>
<p>I grafici parlano da soli.<br />
L&#8217;unico commento che posso fare riguarda la versione 1.9.2 di ruby che ha evidenti problemi di IO e questi risultati non sono in linea con le performance generali di questo linguaggio che, come ho potuto rilevare da altri precedenti test, sono invece molto buone.</p>
<p><img src="http://i.imgur.com/48pyUl.jpg" alt="" title="2,6 Gb" /></p>
<p><img src="http://i.imgur.com/6xHd7l.jpg" alt="" title="880 Mb" /></p>
<p><img src="http://i.imgur.com/2leKpl.jpg" alt="" title="330 Mb" /></p>
<p><img src="http://i.imgur.com/Grrgjl.jpg" alt="" title="All three files" /></p>
<p>Questi sono gli script che ho scritto per l&#8217;occasione:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Written by Marco Mastrodonato on 19/04/2011</span>
<span style="color:#008000; font-style:italic;"># Script to split a file into n output files</span>
<span style="color:#008000; font-style:italic;"># Example:</span>
<span style="color:#008000; font-style:italic;"># ruby split.rb par1 par2</span>
<span style="color:#008000; font-style:italic;"># par1 =&gt; name [default =&gt; input1.txt]</span>
<span style="color:#008000; font-style:italic;"># par2 =&gt; record number that determines the number of output files [default =&gt; 1650]</span>
&nbsp;
strinput = ARGV<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;">||</span> <span style="color:#996600;">'input1.txt'</span>
nrec_to_split = ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> ? ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_i</span> : <span style="color:#006666;">1650</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>? strinput
	<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;File #{strinput} doesn't exists!&quot;</span> 
	<span style="color:#CC0066; font-weight:bold;">exit</span> <span style="color:#006666;">1</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
stroutput = <span style="color:#996600;">&quot;out_%03d.txt&quot;</span>
&nbsp;
t1= <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Ruby #{RUBY_VERSION} #{strinput} started at #{t1}, wait please...&quot;</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>strinput, <span style="color:#996600;">&quot;r&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>f<span style="color:#006600; font-weight:bold;">|</span>
	nsplit = <span style="color:#006666;">0</span>
	nrec = <span style="color:#006666;">0</span>
	fileoutput = <span style="color:#0000FF; font-weight:bold;">nil</span>
&nbsp;
	<span style="color:#9966CC; font-weight:bold;">while</span> line = f.<span style="color:#CC0066; font-weight:bold;">gets</span>
		<span style="color:#9966CC; font-weight:bold;">if</span> nrec <span style="color:#006600; font-weight:bold;">%</span> nrec_to_split == <span style="color:#006666;">0</span>
			nsplit <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
			fileoutput.<span style="color:#9900CC;">close</span> <span style="color:#9966CC; font-weight:bold;">if</span> fileoutput
			fileoutput = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>stroutput <span style="color:#006600; font-weight:bold;">%</span> nsplit, <span style="color:#996600;">'w'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
		<span style="color:#9966CC; font-weight:bold;">end</span>
		fileoutput.<span style="color:#9900CC;">write</span> line
		nrec <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
	fileoutput.<span style="color:#9900CC;">close</span> <span style="color:#9966CC; font-weight:bold;">if</span> fileoutput
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Ended at #{Time.now}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Elapsed time #{Time.now - t1}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">exit</span> <span style="color:#006666;">0</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Written by Marco Mastrodonato on 19/04/2011</span>
<span style="color: #808080; font-style: italic;"># Script to split a file into n output files</span>
<span style="color: #808080; font-style: italic;"># Example:</span>
<span style="color: #808080; font-style: italic;"># python split.py par1 par2</span>
<span style="color: #808080; font-style: italic;"># par1 =&gt; name [default =&gt; input1.txt]</span>
<span style="color: #808080; font-style: italic;"># par2 =&gt; record number that determines the number of output files [default =&gt; 1650]</span>
&nbsp;
<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>, gmtime, strftime
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
	strinput = <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">except</span>:
	strinput = <span style="color: #483d8b;">'input1.txt'</span>
&nbsp;
stroutput = <span style="color: #483d8b;">&quot;out_%03d.txt&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
	nrec_to_split = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">except</span>:
	nrec_to_split = <span style="color: #ff4500;">1650</span>
&nbsp;
t1 = <span style="color: #dc143c;">time</span><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: #dc143c;">sys</span>.<span style="color: black;">version</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span>strftime<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Started at %a, %d %b %Y %H:%M:%S +0000, wait please...&quot;</span>, gmtime<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
nrec = <span style="color: #ff4500;">0</span>
nsplit = <span style="color: #ff4500;">0</span>
&nbsp;
<span style="color: #dc143c;">fileinput</span> = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>strinput, <span style="color: #483d8b;">&quot;r&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">fileinput</span>:
	<span style="color: #ff7700;font-weight:bold;">if</span> nrec <span style="color: #66cc66;">%</span> nrec_to_split == <span style="color: #ff4500;">0</span>:
		<span style="color: #ff7700;font-weight:bold;">try</span>:
			fileoutput.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">NameError</span>:
			fileoutput = <span style="color: #008000;">None</span>
		nsplit += <span style="color: #ff4500;">1</span>
		fileoutput = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>stroutput <span style="color: #66cc66;">%</span>nsplit , <span style="color: #483d8b;">&quot;w&quot;</span><span style="color: black;">&#41;</span>
	fileoutput.<span style="color: black;">write</span><span style="color: black;">&#40;</span>line<span style="color: black;">&#41;</span>
	nrec += <span style="color: #ff4500;">1</span>    
fileoutput.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">fileinput</span>.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span>strftime<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Ended at %a, %d %b %Y %H:%M:%S +0000&quot;</span>, gmtime<span style="color: black;">&#40;</span><span style="color: black;">&#41;</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;Elapsed time %f&quot;</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - t1<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Written by Marco Mastrodonato on 19/04/2011</span>
<span style="color: #666666; font-style: italic;">// Script to split a file into n output files</span>
<span style="color: #666666; font-style: italic;">// Example:</span>
<span style="color: #666666; font-style: italic;">// php split.php par1 par2</span>
<span style="color: #666666; font-style: italic;">// par1 =&gt; name [default =&gt; input1.txt]</span>
<span style="color: #666666; font-style: italic;">// par2 =&gt; record number that determines the number of output files [default =&gt; 1650]</span>
&nbsp;
<span style="color: #000088;">$strinput</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'input1.txt'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$nrec_to_split</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1650</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$stroutput</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'out_%03d.txt'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$t1</span> <span style="color: #339933;">=</span> microtime_float<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Php &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">phpversion</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; started at &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'D, d M Y H:i:s T'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;, wait please...<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$nsplit</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$nrec</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fileinput</span><span style="color: #339933;">=</span><span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$strinput</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">feof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileinput</span><span style="color: #009900;">&#41;</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;">$nrec</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$nrec_to_split</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: #339933;">++</span><span style="color: #000088;">$nsplit</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileoutput</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileoutput</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$fileoutput</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stroutput</span><span style="color: #339933;">,</span> <span style="color: #000088;">$nsplit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$buffer</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fgets</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileinput</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileoutput</span><span style="color: #339933;">,</span> <span style="color: #000088;">$buffer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">++</span><span style="color: #000088;">$nrec</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">fclose</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileinput</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Ended at &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'D, d M Y H:i:s T'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> 
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Elapsed time &quot;</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span>microtime_float<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$t1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> microtime_float<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$usec</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sec</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$usec</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$sec</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;">?&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">--[[
Written by Marco Mastrodonato on 19/04/2011
Script to split a file into n output files
Example:
lua split.lua par1 par2
par1 =&gt; name [default =&gt; input1.txt]
par2 =&gt; record number that determines the number of output files [default =&gt; 1650]
--]]</span>
strinput <span style="color: #66cc66;">=</span> arg <span style="color: #b1b100;">and</span> arg<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #b1b100;">or</span> <span style="color: #ff0000;">&quot;input1.txt&quot;</span>
stroutput <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;out_%03d.txt&quot;</span>
nrec_to_split <span style="color: #66cc66;">=</span> arg <span style="color: #b1b100;">and</span> arg<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> <span style="color: #b1b100;">and</span> <span style="color: #b1b100;">tonumber</span><span style="color: #66cc66;">&#40;</span>arg<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #cc66cc;">1650</span>
&nbsp;
<span style="color: #b1b100;">local</span> t1 <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;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">_VERSION</span> .. <span style="color: #ff0000;">&quot; started at &quot;</span> .. <span style="color: #b1b100;">os.date</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;%a, %d %b %Y %H:%M:%S +0000&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
nsplit <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
nrec <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
<span style="color: #b1b100;">for</span> line <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">io.lines</span><span style="color: #66cc66;">&#40;</span>strinput<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
  <span style="color: #b1b100;">if</span> nrec <span style="color: #66cc66;">%</span> nrec_to_split <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #b1b100;">then</span>
    <span style="color: #b1b100;">if</span> fileOut ~<span style="color: #66cc66;">=</span> <span style="color: #b1b100;">nil</span> <span style="color: #b1b100;">then</span> <span style="color: #b1b100;">io.close</span><span style="color: #66cc66;">&#40;</span>fileOut<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">end</span>
    nsplit <span style="color: #66cc66;">=</span> nsplit + <span style="color: #cc66cc;">1</span>
    fileOut <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">io.open</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">string.format</span><span style="color: #66cc66;">&#40;</span>stroutput, nsplit<span style="color: #66cc66;">&#41;</span> , <span style="color: #ff0000;">'w'</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">end</span>
  fileOut:<span style="color: #b1b100;">write</span> <span style="color: #66cc66;">&#40;</span>line .. <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #66cc66;">&#41;</span>
  nrec <span style="color: #66cc66;">=</span> nrec + <span style="color: #cc66cc;">1</span>
<span style="color: #b1b100;">end</span>
&nbsp;
<span style="color: #b1b100;">io.close</span><span style="color: #66cc66;">&#40;</span>fileOut<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Ended at &quot;</span> .. <span style="color: #b1b100;">os.date</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;%a, %d %b %Y %H:%M:%S +0000&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">print</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;Elapsed time: %.2f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, <span style="color: #b1b100;">os.clock</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> - t1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Per creare i files ho usato questo semplice script ruby:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Example:</span>
<span style="color:#008000; font-style:italic;"># ruby new.rb [NOME] [LINES] [RECORD SIZE]</span>
&nbsp;
stroutput = ARGV<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;">||</span> <span style="color:#996600;">'input1.txt'</span>
num = ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> ? ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_i</span> : <span style="color:#006666;">185000</span>
size = ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span> ? ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_i</span> : <span style="color:#006666;">1799</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>? stroutput
	<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;File #{stroutput} already exists!&quot;</span> 
	<span style="color:#CC0066; font-weight:bold;">exit</span> <span style="color:#006666;">1</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
t1= <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Ruby #{RUBY_VERSION} #{stroutput} started at #{t1}, wait please...&quot;</span>
&nbsp;
line = <span style="color:#996600;">&quot;*&quot;</span> <span style="color:#006600; font-weight:bold;">*</span> size
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>stroutput, <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> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
	num.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span>
		f.<span style="color:#CC0066; font-weight:bold;">puts</span> line
	<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;">&quot;Ended at #{Time.now}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Elapsed time #{Time.now - t1}&quot;</span>
<span style="color:#CC0066; font-weight:bold;">exit</span> <span style="color:#006666;">0</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2011/04/ruby-python-php-lua-comparazione-files-grandi-dimensioni/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Pluralizzare in tutte le lingue</title>
		<link>http://mastrodonato.info/index.php/2010/09/pluralizzare-in-tutte-le-lingue/</link>
		<comments>http://mastrodonato.info/index.php/2010/09/pluralizzare-in-tutte-le-lingue/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 10:33:46 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[I18n]]></category>
		<category><![CDATA[Internazionalizzazione]]></category>
		<category><![CDATA[Pluralize]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=612</guid>
		<description><![CDATA[In questo articolo mostrerò come utilizzare quel comodo helper di rails, pluralize, con tutte le lingue mediante l&#8217;utilizzo di I18n. Diamo per scontato che l&#8217;applicazione sia internazionalizzata, quindi modelli e campi presenti nei file yaml di configurazione. Ora dobbiamo aggiungere i termini per i quali vogliamo internazionalizzare la pluralità: modelli, attributi ecc. activerecord: &#38;activerecord models: [...]]]></description>
			<content:encoded><![CDATA[<p>In questo articolo mostrerò come utilizzare quel comodo helper di rails, <strong>pluralize</strong>, con tutte le lingue mediante l&#8217;utilizzo di I18n.</p>
<p>Diamo per scontato che l&#8217;applicazione sia internazionalizzata, quindi modelli e campi presenti nei file yaml di configurazione.<br />
Ora dobbiamo aggiungere i termini per i quali vogliamo internazionalizzare la pluralità: modelli, attributi ecc.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">activerecord: <span style="color:#006600; font-weight:bold;">&amp;</span>activerecord
    models: <span style="color:#006600; font-weight:bold;">&amp;</span>models
      user: <span style="color:#996600;">&quot;Utente&quot;</span>
      users: <span style="color:#996600;">&quot;Utenti&quot;</span>
      activity: <span style="color:#996600;">&quot;Impiego&quot;</span>
      activities: <span style="color:#996600;">&quot;Impieghi&quot;</span>
      task: <span style="color:#996600;">&quot;Incarico&quot;</span>
      tasks: <span style="color:#996600;">&quot;Incarichi&quot;</span>
      project: <span style="color:#996600;">&quot;Progetto&quot;</span>
      projects: <span style="color:#996600;">&quot;Progetti&quot;</span>
&nbsp;
    attributes: <span style="color:#006600; font-weight:bold;">&amp;</span>attributes
      activity:
        task: <span style="color:#996600;">&quot;Incarico&quot;</span>
        day: <span style="color:#996600;">&quot;Giorno&quot;</span>
        hours: <span style="color:#996600;">&quot;Ore&quot;</span>
        hour: <span style="color:#996600;">&quot;Ora&quot;</span>
        description: <span style="color:#996600;">&quot;Descrizione&quot;</span></pre></div></div>

<p>Ipotizziamo che il nostro modello &#8220;Incarico&#8221; sia relazionato col modello &#8220;Impiego&#8221; e che questo abbia un campo di nome &#8220;Ore&#8221;. Vogliamo creare una frase che descriva quanti impieghi sono presenti per tale incarico ed il totale delle ore.</p>
<p>In base ai dati nel database, vogliamo ottenere frasi del tipo:<br />
<em>Questo incarico ha <strong>1 impiego</strong> per un totale di <strong>1 ora</strong>.<br />
Questo incarico ha <strong>13 impieghi</strong> per un totale di <strong>0 ore</strong>.<br />
Questo incarico ha <strong>1 impiego</strong> per un totale di <strong>15 ore</strong>.<br />
</em></p>
<p>&#8230;e in tutte le lingue:<br />
<em><br />
This task has <strong>1 activity</strong> with an amount of <strong>15 hours</strong>.<br />
Diese Aufgabe haben <strong>1-Aktivität</strong> für eine Gesamtmenge von <strong>15 Stunden</strong>.<br />
Cette tâche a <strong>1 activité</strong> pour un total de <strong>15 heures</strong>.<br />
Esta tarea tiene <strong>1 actividad</strong> para un total de <strong>15 horas</strong>.<br />
このタスクは、 15時間の量1活性を持つ</em></p>
<p>Vogliamo evitare:<br />
<del>Questo incarico ha 1 impieghi per un totale di 1 ore</del><br />
<del>This task has 1 activities with an amount of 1 hours</del></p>
<p>Aggiungiamo la frase in ogni file yaml I18n:</p>

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

<p>Per fare ciò ci avvaliamo del comodo helper di rails, pluralize, opportunamente modificato per eliminare il numero dal risultato. Pluralizziamo il termine inglese da usare come chiave per I18n.</p>
<p>Creiamo nell&#8217;helper dell&#8217;applicazione:</p>

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

<p>Nella view richiamiamo la frase I18n passando il numero di impieghi ed il totale di ore (per motivi descrittivi ho inserito nella view operazioni che però riterrei più opportuno posizionare nel controller):</p>

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

<p>Osserviamo questa riga alla moviola:</p>

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

<p>In base al numero di ore otteniamo la chiave I18n: hour o hours</p>

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

<p>equivale</p>

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

<p>se tot_hours = 1 avremo:</p>

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

<p>altrimenti:</p>

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

<p>Questo è il risultato:<br />
<a href="http://mastrodonato.info/wp-content/uploads/2010/09/Esempio_Pluralize_I18n.png"><img src="http://mastrodonato.info/wp-content/uploads/2010/09/Esempio_Pluralize_I18n-300x248.png" alt="Esempio Pluralize I18n" title="Esempio Pluralize I18n" width="300" height="248" class="aligncenter size-medium wp-image-614" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2010/09/pluralizzare-in-tutte-le-lingue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby 1.9.2 prestazioni al top</title>
		<link>http://mastrodonato.info/index.php/2010/09/ruby-1-9-2-prestazioni-al-top/</link>
		<comments>http://mastrodonato.info/index.php/2010/09/ruby-1-9-2-prestazioni-al-top/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 20:29:32 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[jRuby]]></category>
		<category><![CDATA[RVM]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=595</guid>
		<description><![CDATA[Con un precedente test, avevo riscontrato che JRuby era la massima espressione per quanto riguardava la velocità nell&#8217;eseguire codice ruby, surclassando non solo gli altri interpreti C++ e .Net ma azzardando prepotenti confronti con le ultime due incarnazioni di python. Gli script erano questi e li ho rispolverati anche per questo nuovo confronto. Ora che [...]]]></description>
			<content:encoded><![CDATA[<p>Con un <a href="http://mastrodonato.info/index.php/2010/03/ruby-vs-python-vs-windows-vs-linux/">precedente test</a>, avevo riscontrato che JRuby era la massima espressione per quanto riguardava la velocità nell&#8217;eseguire codice ruby, surclassando non solo gli altri interpreti C++ e .Net ma azzardando prepotenti confronti con le ultime due incarnazioni di python. Gli <a href="http://mastrodonato.info/index.php/2010/01/ruby-vs-ruby-vs-python-vs-python/">script</a> erano questi e li ho rispolverati anche per questo nuovo confronto.</p>
<p>Ora che Rails 3 è stato rilasciato ed uno degli interpreti consigliati è il nuovo 1.9.2, ero curioso di osservare come si comporta la release ufficiale: direi davvero niente male!<br />
Questa volta ho ristretto il numero dei partecipanti alle sole versioni installate con <a href="http://rvm.beginrescueend.com/">RVM</a> più un paio di versioni windows: JRuby 1.4.0 ed il più usato 1.8.7 mingw32, giusto per avere un metro di paragone.</p>
<p>Distribuzione Linux: Ubuntu 10.4 64bit<br />
Windows XP 32Bit SP3</p>
<p><center></p>
<table>
<th>Versione</th>
<th>Compilatore/Sistema</th>
<th>Secondi</th>
<tbody>
<tr>
<td>Ruby 1.9.2 p0 RVM</td>
<td>x86_64-linux</td>
<td><strong>6,6</strong></td>
</tr>
<tr>
<td>JRuby 1.4.0 RVM</td>
<td>OpenJDK 64-Bit Server VM 1.6.0_18 [amd64-java]</td>
<td>6,8</td>
</tr>
<tr>
<td>JRuby 1.5.2 RVM</td>
<td>OpenJDK 64-Bit Server VM 1.6.0_18 [amd64-java]</td>
<td>7,0</td>
</tr>
<tr>
<td>JRuby 1.4.0 </td>
<td>Windows Client VM 1.6.0_15 [x86-java]</td>
<td>7,0</td>
</tr>
<tr>
<td>Ruby 1.9.1 p378 RVM</td>
<td>x86_64-linux</td>
<td>8,9</td>
</tr>
<tr>
<td>Ruby 1.8.7 p249 RVM</td>
<td>x86_64-linux</td>
<td>12,2</td>
</tr>
<tr>
<td>Ruby 1.8.7 p249</td>
<td>i386-mingw32</td>
<td>23,9</td>
</tr>
</tbody>
</table>
<p></center></p>
<p><a href="http://mastrodonato.info/wp-content/uploads/2010/09/BenchMM1_20100906.png"><img src="http://mastrodonato.info/wp-content/uploads/2010/09/BenchMM1_20100906-300x150.png" alt="" title="BenchMM1 06/09/2010" width="300" height="150" class="aligncenter size-medium wp-image-596" /></a></p>
<p>Questo il log completo dell&#8217;esecuzione:</p>
<p><code><br />
marco@d9400:~$ rvm list</p>
<p>rvm rubies</p>
<p>=> jruby-1.4.0 [ amd64-java ]<br />
   jruby-1.5.2 [ amd64-java ]<br />
   ruby-1.8.7-p249 [ x86_64 ]<br />
   ruby-1.9.1-p378 [ x86_64 ]<br />
   ruby-1.9.2-p0 [ x86_64 ]</p>
<p>marco@d9400:~$ java -version<br />
java version "1.6.0_18"<br />
OpenJDK Runtime Environment (IcedTea6 1.8.1) (6b18-1.8.1-0ubuntu1)<br />
OpenJDK 64-Bit Server VM (build 16.0-b13, mixed mode)</p>
<p>marco@d9400:~$ ruby -v<br />
jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (OpenJDK 64-Bit Server VM 1.6.0_18) [amd64-java]<br />
marco@d9400:~$ ruby RubyMM186_3.rb --server --fast</p>
<p>Warming up...<br />
Strings test - Elapsed 2.096<br />
Check1: 50000<br />
Check2: 157490<br />
Check3: 50005<br />
Check4: 373847<br />
Arrays test  - Elapsed 3.508<br />
Check1: 20900<br />
Check2: 250000<br />
Check3: 250000<br />
Check4: 1500000<br />
Check5: 5749950<br />
Numeric test - Elapsed 2.856<br />
Check1: 6252500<br />
Check2: 1439295494700374021157505910939096377494040420940312<br />
Ruby Partial elapsed time 8.460</p>
<p>1. Starting Ruby tests...<br />
Strings test - Elapsed 1.501<br />
Arrays test  - Elapsed 3.054<br />
Numeric test - Elapsed 2.280<br />
Ruby Partial elapsed time 6.835</p>
<p>2. Starting Ruby tests...<br />
Strings test - Elapsed 1.431<br />
Arrays test  - Elapsed 3.091<br />
Numeric test - Elapsed 2.249<br />
Ruby Partial elapsed time 6.771</p>
<p>3. Starting Ruby tests...<br />
Strings test - Elapsed 1.505<br />
Arrays test  - Elapsed 2.999<br />
Numeric test - Elapsed 2.261<br />
Ruby Partial elapsed time 6.765<br />
-------------------------------------<br />
Average Strings test - Elapsed 1.479<br />
Average Arrays test  - Elapsed 3.048<br />
Average Numeric test - Elapsed 2.263</p>
<p>Ruby Average elapsed time 6.790</p>
<p>marco@d9400:~$ rvm use jruby-1.5.2<br />
marco@d9400:~$ ruby -v<br />
jruby 1.5.2 (ruby 1.8.7 patchlevel 249) (2010-08-20 1c5e29d) (OpenJDK 64-Bit Server VM 1.6.0_18) [amd64-java]<br />
marco@d9400:~$ ruby RubyMM186_3.rb --server --fast</p>
<p>Warming up...<br />
Strings test - Elapsed 2.383<br />
Check1: 50000<br />
Check2: 157490<br />
Check3: 50005<br />
Check4: 373847<br />
Arrays test  - Elapsed 3.561<br />
Check1: 20900<br />
Check2: 250000<br />
Check3: 250000<br />
Check4: 1500000<br />
Check5: 5749950<br />
Numeric test - Elapsed 3.210<br />
Check1: 6252500<br />
Check2: 1439295494700374021157505910939096377494040420940312<br />
Ruby Partial elapsed time 9.154</p>
<p>1. Starting Ruby tests...<br />
Strings test - Elapsed 1.479<br />
Arrays test  - Elapsed 3.092<br />
Numeric test - Elapsed 2.516<br />
Ruby Partial elapsed time 7.087</p>
<p>2. Starting Ruby tests...<br />
Strings test - Elapsed 1.431<br />
Arrays test  - Elapsed 3.056<br />
Numeric test - Elapsed 2.508<br />
Ruby Partial elapsed time 6.995</p>
<p>3. Starting Ruby tests...<br />
Strings test - Elapsed 1.423<br />
Arrays test  - Elapsed 3.053<br />
Numeric test - Elapsed 2.524<br />
Ruby Partial elapsed time 7.000<br />
-------------------------------------<br />
Average Strings test - Elapsed 1.444<br />
Average Arrays test  - Elapsed 3.067<br />
Average Numeric test - Elapsed 2.516</p>
<p>Ruby Average elapsed time 7.027</p>
<p>marco@d9400:~$ rvm use ruby-1.8.7-p249<br />
marco@d9400:~$ ruby -v<br />
ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]<br />
marco@d9400:~$ ruby RubyMM186_3.rb</p>
<p>Warming up...<br />
Strings test - Elapsed 3.135<br />
Check1: 50000<br />
Check2: 157490<br />
Check3: 50005<br />
Check4: 373847<br />
Arrays test  - Elapsed 4.386<br />
Check1: 20900<br />
Check2: 250000<br />
Check3: 250000<br />
Check4: 1500000<br />
Check5: 5749950<br />
Numeric test - Elapsed 4.629<br />
Check1: 6252500<br />
Check2: 1439295494700374021157505910939096377494040420940312<br />
Ruby Partial elapsed time 12.149</p>
<p>1. Starting Ruby tests...<br />
Strings test - Elapsed 3.181<br />
Arrays test  - Elapsed 4.402<br />
Numeric test - Elapsed 4.598<br />
Ruby Partial elapsed time 12.180</p>
<p>2. Starting Ruby tests...<br />
Strings test - Elapsed 3.153<br />
Arrays test  - Elapsed 4.387<br />
Numeric test - Elapsed 4.572<br />
Ruby Partial elapsed time 12.113</p>
<p>3. Starting Ruby tests...<br />
Strings test - Elapsed 3.210<br />
Arrays test  - Elapsed 4.395<br />
Numeric test - Elapsed 4.583<br />
Ruby Partial elapsed time 12.189<br />
-------------------------------------<br />
Average Strings test - Elapsed 3.181<br />
Average Arrays test  - Elapsed 4.395<br />
Average Numeric test - Elapsed 4.584</p>
<p>Ruby Average elapsed time 12.160</p>
<p>marco@d9400:~$ rvm use ruby-1.9.1<br />
marco@d9400:~$ ruby -v<br />
ruby 1.9.1p378 (2010-01-10 revision 26273) [x86_64-linux]<br />
marco@d9400:~$ ruby RubyMM191_3.rb</p>
<p>Warming up...<br />
Strings test - Elapsed 2.452<br />
Check1: 50000<br />
Check2: 157490<br />
Check3: 50005<br />
Check4: 373847<br />
Arrays test  - Elapsed 4.488<br />
Check1: 20900<br />
Check2: 250000<br />
Check3: 250000<br />
Check4: 1500000<br />
Check5: 5749950<br />
Numeric test - Elapsed 1.840<br />
Check1: 6252500<br />
Check2: 1439295494700374021157505910939096377494040420940312<br />
Ruby Partial elapsed time 8.780</p>
<p>1. Starting Ruby tests...<br />
Strings test - Elapsed 2.314<br />
Arrays test  - Elapsed 4.679<br />
Numeric test - Elapsed 1.843<br />
Ruby Partial elapsed time 8.836</p>
<p>2. Starting Ruby tests...<br />
Strings test - Elapsed 2.319<br />
Arrays test  - Elapsed 4.691<br />
Numeric test - Elapsed 1.843<br />
Ruby Partial elapsed time 8.853</p>
<p>3. Starting Ruby tests...<br />
Strings test - Elapsed 2.318<br />
Arrays test  - Elapsed 4.705<br />
Numeric test - Elapsed 1.847<br />
Ruby Partial elapsed time 8.870<br />
-------------------------------------<br />
Average Strings test - Elapsed 2.317<br />
Average Arrays test  - Elapsed 4.692<br />
Average Numeric test - Elapsed 1.844</p>
<p>Ruby Average elapsed time 8.853</p>
<p>marco@d9400:~$ rvm use ruby-1.9.2<br />
marco@d9400:~$ ruby -v<br />
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]<br />
marco@d9400:~$ ruby RubyMM191_3.rb</p>
<p>Warming up...<br />
Strings test - Elapsed 1.680<br />
Check1: 50000<br />
Check2: 157490<br />
Check3: 50005<br />
Check4: 373847<br />
Arrays test  - Elapsed 4.192<br />
Check1: 20900<br />
Check2: 250000<br />
Check3: 250000<br />
Check4: 1500000<br />
Check5: 5749950<br />
Numeric test - Elapsed 1.708<br />
Check1: 6252500<br />
Check2: 1439295494700374021157505910939096377494040420940312<br />
Ruby Partial elapsed time 7.579</p>
<p>1. Starting Ruby tests...<br />
Strings test - Elapsed 1.654<br />
Arrays test  - Elapsed 3.178<br />
Numeric test - Elapsed 1.704<br />
Ruby Partial elapsed time 6.536</p>
<p>2. Starting Ruby tests...<br />
Strings test - Elapsed 1.746<br />
Arrays test  - Elapsed 3.388<br />
Numeric test - Elapsed 1.699<br />
Ruby Partial elapsed time 6.833</p>
<p>3. Starting Ruby tests...<br />
Strings test - Elapsed 1.644<br />
Arrays test  - Elapsed 3.227<br />
Numeric test - Elapsed 1.706<br />
Ruby Partial elapsed time 6.577<br />
-------------------------------------<br />
Average Strings test - Elapsed 1.681<br />
Average Arrays test  - Elapsed 3.264<br />
Average Numeric test - Elapsed 1.703</p>
<p>Ruby Average elapsed time 6.649<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2010/09/ruby-1-9-2-prestazioni-al-top/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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 [...]]]></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 [...]]]></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. [...]]]></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>
    <span style="color:#006666;">29</span>.<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>
    <span style="color:#006666;">0</span>.<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>
    <span style="color:#006666;">5000</span>.<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>
    <span style="color:#006666;">31</span>.<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><span style="color:#006666;">8</span>..<span style="color:#006666;">95</span><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> <span style="color:#006666;">2</span>..<span style="color:#006666;">42</span>
      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><span style="color:#006666;">2</span>..<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><span style="color:#006666;">2</span>..<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><span style="color:#006666;">1</span>..<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>
    <span style="color:#006666;">29</span>.<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>
    <span style="color:#006666;">0</span>.<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>
    <span style="color:#006666;">5000</span>.<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>
    <span style="color:#006666;">31</span>.<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><span style="color:#006666;">8</span>..<span style="color:#006666;">95</span><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> <span style="color:#006666;">2</span>..<span style="color:#006666;">42</span>
      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><span style="color:#006666;">2</span>..<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><span style="color:#006666;">2</span>..<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><span style="color:#006666;">1</span>..<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>
	</channel>
</rss>

