<?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>Blog::Quibb &#187; ruby</title>
	<atom:link href="http://blog.quibb.org/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.quibb.org</link>
	<description>Software development and more.</description>
	<lastBuildDate>Tue, 10 Aug 2010 14:11:56 +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>Regular Expressions Review</title>
		<link>http://blog.quibb.org/2009/02/regular-expressions-review/</link>
		<comments>http://blog.quibb.org/2009/02/regular-expressions-review/#comments</comments>
		<pubDate>Sat, 14 Feb 2009 19:18:47 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.quibb.org/?p=25</guid>
		<description><![CDATA[A hobby of mine is to learn new programming languages.  I try and learn at least one a year, and use it for more than just a hello world app.  So this year is the year of python, where if I&#8217;m required to write a script Python is the go to guy.  Having that said, [...]]]></description>
			<content:encoded><![CDATA[<p>A hobby of mine is to learn new programming languages.  I try and learn at least one a year, and use it for more than just a hello world app.  So this year is the year of python, where if I&#8217;m required to write a script Python is the go to guy.  Having that said, I recently had a need for regular expressions, so python was used.</p>
<p>Being most familiar with Java and Ruby, Python seems a little in between, but one feature that stuck out was the regular expression syntax.  Python let&#8217;s you put a &#8216;r&#8217; in front of the quote to denote raw input.  This means you don&#8217;t have to escape back slashes twice (ala Java).</p>
<p>For those not familiar with regular expressions, here is an example of a regular expression in a few languages:</p>
<pre style="padding-left: 30px;">Java:
Find Slashes: "[\\\\/]"

Python:
Find Slashes: r"[\\/]"

Ruby:
Find Slashes: /[\\\/]/</pre>
<p>This is just a simple example to illustrate a point, but look at Java.  Find slashes has 4 backslashes.  Even if regular expressions were typically this simple (they&#8217;re not), that seems unnecessary.  Anytime it&#8217;s necessary for a backslash to make it to the regular expression processor, there much be two in the regular expression.  Another quick example, to find a period (.) the regular expression would be &#8220;\\.&#8221;.  This compounds very quickly, and makes it painful to use regular expressions in Java.</p>
<p>I can&#8217;t understand why Java wouldn&#8217;t adopt the python syntax of putting a &#8216;r&#8217; in from of a string to denote raw input.  At quick glance, it doesn&#8217;t seem as though it would break any currently in use regular expressions because the old syntax would continue working as expected.  It&#8217;d make them better for the future.</p>
<p>Here are some Regular Expression resources that I find useful:</p>
<p><a title="http://www.rubular.com/" href="http://www.rubular.com/">http://www.rubular.com/</a> A Ruby Regular Expression Tester</p>
<p><a title="http://www.fileformat.info/tool/regex.htm" href="http://www.fileformat.info/tool/regex.htm">http://www.fileformat.info/tool/regex.htm</a> A Java Regular Expression Tester</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.quibb.org/2009/02/regular-expressions-review/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Shootout: Fasta</title>
		<link>http://blog.quibb.org/2008/11/ruby-shootout-fasta/</link>
		<comments>http://blog.quibb.org/2008/11/ruby-shootout-fasta/#comments</comments>
		<pubDate>Sun, 02 Nov 2008 13:39:34 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[benchmarks]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[shootout]]></category>

		<guid isPermaLink="false">http://blog.quibb.org/?p=3</guid>
		<description><![CDATA[Lately, I&#8217;ve been looking at the shootout Ruby benchmarks. I&#8217;d gotten into a habit of checking them every few months, rooting for my favorite languages. Not really understanding why some didn&#8217;t have the greatest showing on there. When the people running it upgraded their hardware, it seems as though Ruby fell off the list. While [...]]]></description>
			<content:encoded><![CDATA[<p>Lately, I&#8217;ve been looking at the <a href="http://shootout.alioth.debian.org/">shootout</a> Ruby benchmarks.  I&#8217;d gotten into a habit of checking them every few months, rooting for my favorite languages.  Not really understanding why some didn&#8217;t have the greatest showing on there.  When the people running it upgraded their hardware, it seems as though Ruby fell off the list.  While Ruby is a slow language, it deserves its spot (even if it is the bottom).  I looked at the submissions and saw a post saying if more Ruby benchmarks were updated it would be included again.</p>
<p>I&#8217;ve since updated 3 of the benchmarks; I sped up reverse-compliment to be 2x faster than the previous.  I&#8217;m not a Ruby expert by any means, but I was able to get a speedup.  I don&#8217;t know if the other languages are in the same state, but they may be&#8230;</p>
<p>Anyway, the Fasta benchmark is the one that I most recently updated.  It took me a while to get a decent speedup.  It turned out the Array#find is slower than Array#each with a break statement.  Making this switch is what got the biggest speedup out of the program.</p>
<p>I wrote a benchmark showing the difference in runtime:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'benchmark'</span>
&nbsp;
N = <span style="color:#006666;">300</span>
table = <span style="color:#006600; font-weight:bold;">&#40;</span>0..300<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_a</span>
<span style="color:#CC00FF; font-weight:bold;">Benchmark</span>.<span style="color:#9900CC;">bmbm</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">8</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>x<span style="color:#006600; font-weight:bold;">|</span>
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Array.find&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
    N.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      table.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>find_num<span style="color:#006600; font-weight:bold;">|</span>
	table.<span style="color:#9900CC;">find</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>num<span style="color:#006600; font-weight:bold;">|</span>
	  num <span style="color:#006600; font-weight:bold;">&gt;</span> find_num
	<span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Array.each&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
    N.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      table.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>find_num<span style="color:#006600; font-weight:bold;">|</span>
	output = <span style="color:#0000FF; font-weight:bold;">nil</span>
	table.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>num<span style="color:#006600; font-weight:bold;">|</span>
	  <span style="color:#9966CC; font-weight:bold;">if</span> num <span style="color:#006600; font-weight:bold;">&gt;</span> find_num <span style="color:#9966CC; font-weight:bold;">then</span>
	    output = num
	    <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>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The results:</p>
<pre>Ruby 1.8.6       user     system      total        real
Array.find  12.120000   0.020000  12.140000 ( 14.647705)
Array.each   8.150000   0.030000   8.180000 (  9.734463)

JRuby 1.1.4      user     system      total        real
Array.find   6.738000   0.000000   6.738000 (  6.738407)
Array.each   5.365000   0.000000   5.365000 (  5.364320)</pre>
<p>Now, I&#8217;m running this on an admittedly dated machine.  Again, I&#8217;m nowhere near a ruby expert, so if someone sees a way to make the benchmark more fair let me know, and I&#8217;ll update it.  I can&#8217;t say anything about how this would run in YARV when that is released.</p>
<p>Here is a link to the Ruby Fasta benchmark on the Language Shootout:</p>
<p><a title="Ruby Shootout: Fasta" href="http://shootout.alioth.debian.org/u32/benchmark.php?test=fasta&amp;lang=ruby&amp;id=1">Ruby Shootout: Fasta</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.quibb.org/2008/11/ruby-shootout-fasta/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
