[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Array performance question

Jim Menard

2/27/2009 1:17:00 PM

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

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
--
Jim Menard, jimm@io.com, jim.menard@gmail.com
http://www.io....

4 Answers

William James

2/28/2009 6:08:00 AM

0

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

}

Ryan Davis

3/1/2009 5:33:00 AM

0


On Feb 27, 2009, at 05:16 , Jim Menard 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...

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

Ryan Davis

3/1/2009 5:34:00 AM

0


On Feb 28, 2009, at 21:32 , Ryan Davis wrote:

>
> On Feb 27, 2009, at 05:16 , Jim Menard 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...
>
> I don't have time to dig into this, but I believe he's hitting a GC
> 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


Thomas Enebo

3/10/2009 2:01:00 PM

0

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