# Array performance question

**URL:** <https://www.ruby-forum.com/t/array-performance-question/161569>\
**Category:** Ruby\
**Created:** [February 27, 2009, 2:18pm UTC](https://www.ruby-forum.com/t/array-performance-question/161569 "2009-02-27T14:18:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wolfmanjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3da27b/32.png) [@wolfmanjm](https://www.ruby-forum.com/u/wolfmanjm)\
**Post date:** [February 27, 2009, 2:18pm UTC](https://www.ruby-forum.com/t/array-performance-question/161569/1 "2009-02-27T14:18:29Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![wolfmanjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3da27b/32.png) [@wolfmanjm](https://www.ruby-forum.com/u/wolfmanjm)\
**Post date:** [February 27, 2009, 3:51pm UTC](https://www.ruby-forum.com/t/array-performance-question/161569/2 "2009-02-27T15:51:30Z")

</div>

2009/2/27 Jim M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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.

Slightly modified test (attached):

## 15:44:06 Temp$ ruby bm.rb bm; ruby bm.rb bmbm; ruby19 bm.rb bm; ruby19 bm.rb bmbm 1.8.7 user system total real size = 10000 10000 0.125000 0.000000 0.125000 ( 0.114000) size = 100000 100000 0.859000 0.000000 0.859000 ( 0.863000) size = 1000000 1000000 24.969000 0.000000 24.969000 ( 24.961000) 1.8.7 Rehearsal

size = 10000 10000 24.235000 0.032000 24.267000 (  
24.308000)  
size = 100000 100000 18.078000 0.000000 18.078000 (  
18.150630)  
size = 1000000 1000000 14.297000 0.000000 14.297000 (  
14.289000)  
------------------------------------------------------- total:  
56.642000sec

```
                                user system total 

```

## real size = 10000 10000 12.031000 0.000000 12.031000 ( 12.039000) size = 100000 100000 10.250000 0.000000 10.250000 ( 10.290950) size = 1000000 1000000 8.985000 0.000000 8.985000 ( 8.989000) 1.9.1 user system total real size = 10000 10000 0.016000 0.000000 0.016000 ( 0.027000) size = 100000 100000 0.031000 0.000000 0.031000 ( 0.028000) size = 1000000 1000000 0.047000 0.000000 0.047000 ( 0.043000) 1.9.1 Rehearsal

size = 10000 10000 0.032000 0.000000 0.032000 (  
0.043000)  
size = 100000 100000 0.032000 0.000000 0.032000 (  
0.043000)  
size = 1000000 1000000 0.031000 0.000000 0.031000 (  
0.043000)  
-------------------------------------------------------- total:  
0.095000sec

```
                                user system total 

```

real  
size = 10000 10000 0.031000 0.000000 0.031000 (  
0.039000)  
size = 100000 100000 0.047000 0.000000 0.047000 (  
0.043000)  
size = 1000000 1000000 0.032000 0.000000 0.032000 (  
0.043000)

Strange that the rehearsal has this dramatic effect. Normally it  
should simply execute the block twice to warm up the system.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![wolfmanjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3da27b/32.png) [@wolfmanjm](https://www.ruby-forum.com/u/wolfmanjm)\
**Post date:** [February 27, 2009, 4:24pm UTC](https://www.ruby-forum.com/t/array-performance-question/161569/3 "2009-02-27T16:24:27Z")

</div>

2009/2/27 Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Strange that the rehearsal has this dramatic effect. Â Normally it  
> should simply execute the block twice to warm up the system.

Here’s a better test

16:16:32 Temp$ ruby bm.rb; ruby19 bm.rb  
1.8.7  
user system total  
real  
size = 10000 10000 0.110000 0.000000 0.110000 (  
0.118000)  
size = 100000 100000 0.781000 0.000000 0.781000 (  
0.769000)  
size = 1000000 1000000 25.172000 0.000000 25.172000 (  
25.185000)  
size = 10000 10000 0.328000 0.000000 0.328000 (  
0.329000)  
size = 100000 100000 0.547000 0.000000 0.547000 (  
0.542000)  
size = 1000000 1000000 18.156000 0.000000 18.156000 (  
18.151000)  
1.9.1  
user system total  
real  
size = 10000 10000 0.031000 0.000000 0.031000 (  
0.027000)  
size = 100000 100000 0.031000 0.000000 0.031000 (  
0.028000)  
size = 1000000 1000000 0.031000 0.000000 0.031000 (  
0.043000)  
size = 10000 10000 0.031000 0.000000 0.031000 (  
0.031000)  
size = 100000 100000 0.032000 0.000000 0.032000 (  
0.023000)  
size = 1000000 1000000 0.031000 0.000000 0.031000 (  
0.043000)

“better” in the sense that we’re not suffering from bmbm’s magic.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![wolfmanjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3da27b/32.png) [@wolfmanjm](https://www.ruby-forum.com/u/wolfmanjm)\
**Post date:** [February 28, 2009, 7:10am UTC](https://www.ruby-forum.com/t/array-performance-question/161569/4 "2009-02-28T07:10:26Z")

</div>

Jim M. wrote:

> 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 )  
}  
}

}

---

<div class="post-metadata">

**Author:** ![wolfmanjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3da27b/32.png) [@wolfmanjm](https://www.ruby-forum.com/u/wolfmanjm)\
**Post date:** [March 1, 2009, 6:34am UTC](https://www.ruby-forum.com/t/array-performance-question/161569/5 "2009-03-01T06:34:27Z")

</div>

On Feb 27, 2009, at 05:16 , Jim M. wrote:

> 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](http://blogs.codehaus.org/people/geir/archives/001768_baffled_with_ruby.html)

I don’t have time to dig into this, but I believe he’s hitting a GC  
threshold by going over N objects.

---

<div class="post-metadata">

**Author:** ![wolfmanjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3da27b/32.png) [@wolfmanjm](https://www.ruby-forum.com/u/wolfmanjm)\
**Post date:** [March 1, 2009, 6:36am UTC](https://www.ruby-forum.com/t/array-performance-question/161569/6 "2009-03-01T06:36:13Z")

</div>

On Feb 28, 2009, at 21:32 , Ryan D. wrote:

> threshold by going over N objects.  
> Poke around gc.c and see what you can figure out. Also, look at the  
> 1.9 version or through the changelog to find hints…

#ifndef GC\_MALLOC\_LIMIT  
#if defined(MSDOS) || defined( **human68k** )  
#define GC\_MALLOC\_LIMIT 200000  
#else  
#define GC\_MALLOC\_LIMIT 8000000  
#endif  
#endif

---

<div class="post-metadata">

**Author:** ![wolfmanjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3da27b/32.png) [@wolfmanjm](https://www.ruby-forum.com/u/wolfmanjm)\
**Post date:** [March 10, 2009, 3:06pm UTC](https://www.ruby-forum.com/t/array-performance-question/161569/7 "2009-03-10T15:06:24Z")

</div>

Jim M. wrote:

> Jim

I am betting Ryan is right about GC issues. JRuby does not fall down  
doing this either (which probably also backs up the GC theory):

ruby ~/jruby/scripts/arr\_ben.rb  
size = 10000 10000  
0.050000 0.000000 0.050000 ( 0.057190)  
size = 100000 100000  
0.660000 0.000000 0.660000 ( 0.672178)  
size = 1000000 1000000  
34.430000 0.340000 34.770000 ( 35.562454)

jruby --server ~/jruby/scripts/arr\_ben.rb  
size = 10000 10000  
0.372000 0.000000 0.372000 ( 0.266000)  
size = 100000 100000  
0.099000 0.000000 0.099000 ( 0.099000)  
size = 1000000 1000000  
0.154000 0.000000 0.154000 ( 0.154000)

-Tom
