<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>loopkid: slow iterators in ruby 1.9 on mac</title>
    <link>http://loopkid.net/articles/2008/06/17/slow-iterators-in-ruby-1-9-on-mac</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>sad songs make me happy</description>
    <item>
      <title>slow iterators in ruby 1.9 on mac</title>
      <description>&lt;p&gt;If you ever wondered why your Ruby programs are not faster but much slower with Ruby 1.9 you might be using &lt;code&gt;each&lt;/code&gt; or &lt;code&gt;times&lt;/code&gt; iterators on large collections.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;10000000.times {}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In fact if you do a benchmark,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require 'benchmark'

n = 10000000

Benchmark.bm do |x|
  x.report { n.times {} }
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;you get stunning results.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ruby bench.rb
   user     system      total        real
   0.720000   0.000000   0.720000 (  0.721872)
$ ruby1.9 bench.rb 
   user     system      total        real
   16.670000   9.110000  25.780000 ( 25.923977)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When increasing the number of iterations by powers of ten it looks like the computational complexity of the &lt;code&gt;times&lt;/code&gt; iterator is O(n) in Ruby 1.9. According to &lt;a href="http://antoniocangiano.com/2008/03/25/inject-each-and-times-methods-much-slower-in-ruby-19/"&gt;Antonio Cangiano&lt;/a&gt; this behaviour is specific to Mac OS X and does not appear on Linux.&lt;/p&gt;</description>
      <pubDate>Tue, 17 Jun 2008 22:45:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:ae74e534-29d8-4b96-9562-7bd1f26e3824</guid>
      <author>Stefan</author>
      <link>http://loopkid.net/articles/2008/06/17/slow-iterators-in-ruby-1-9-on-mac</link>
      <category>English</category>
      <category>Mac OS X</category>
      <category>Ruby</category>
      <trackback:ping>http://loopkid.net/articles/trackback/8201</trackback:ping>
    </item>
  </channel>
</rss>
