Array performance question

Discussion in 'Ruby' started by Jim Menard, Feb 27, 2009.

  1. Jim Menard

    Jim Menard Guest

    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
    --
    Jim Menard, ,
    http://www.io.com/~jimm/
     
    Jim Menard, Feb 27, 2009
    #1
    1. Advertising

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

    }
     
    William James, Feb 28, 2009
    #2
    1. Advertising

  3. Jim Menard

    Ryan Davis Guest

    Ryan Davis, Mar 1, 2009
    #3
  4. Jim Menard

    Ryan Davis Guest

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


    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
     
    Ryan Davis, Mar 1, 2009
    #4
  5. Jim Menard

    Thomas Enebo Guest

    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
    >

    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
     
    Thomas Enebo, Mar 10, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jm
    Replies:
    1
    Views:
    529
    alien2_51
    Dec 12, 2003
  2. Maxim Khitrov

    Strange array.array performance

    Maxim Khitrov, Feb 19, 2009, in forum: Python
    Replies:
    0
    Views:
    209
    Maxim Khitrov
    Feb 19, 2009
  3. Maxim Khitrov

    Re: Strange array.array performance

    Maxim Khitrov, Feb 19, 2009, in forum: Python
    Replies:
    5
    Views:
    390
  4. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    512
    David RF
    Sep 3, 2009
  5. Software Engineer
    Replies:
    0
    Views:
    357
    Software Engineer
    Jun 10, 2011
Loading...

Share This Page