From: William James Date: 2009-02-28T15:09:03+09:00 Subject: Re: Array performance question Jim Menard wrote: > Here's a real Array performance mystery. I can't help my friend figure > this one out. He writes, > > ======== > > Is there a known bug in Ruby array performance? I spent a lot of time > yesterday boiling down the following example > > > http://blogs.codehaus.org/people/geir/archives/001768_baffled_with_ruby.html > > only because I still can't believe I'm not doing something incredibly > stupid. (I'm a newbie, "Reluctant Rubyist") > > I'm just not used to arrays behaving this way :) Anyone have any > insight? > > ======== > > He'd love a response in his blog, but with permission I'll copy > answers here over there. He's not a regular ruby-talk reader...yet. > > Jim This variation exhibits the same behavior. require 'benchmark' class BufferContainer def initialize( initial_data ) @buf = initial_data end def put_array( array ) @buf[0, array.size] = array end end size = 999 the_array = (0..4).to_a (1..3).each{|i| size *= 10 a = [nil] * size # RUN WITH THIS COMMENTED OUT FIRST # a << 9 puts "size = #{ size } #{ a.size }" puts Benchmark.measure { 10_000.times { buf = BufferContainer.new( a ) buf.put_array( the_array ) } } }