Array performance question

J

Jim Menard

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
 
W

William James

Jim said:
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 )
}
}

}
 
R

Ryan Davis

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
 
T

Thomas Enebo

Jim said:
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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top