On Sun, Nov 8, 2009 at 6:38 AM, lith <minilith / gmail.com> wrote:

> > "Inefficient" can also refer to memory.  If the array has 4 million
> > entries, to copy it 100 times might be considered inefficient in that
> > sense.
>
> Well, maybe you shouldn't use an array then anyway -- which is more or
> less what you're doing by introducing a layer of indirection.
>
> BTW how does your approach behave if you delete 4 million from
> MyArray? I'd rather go for something tree-like and exchange selected
> branches, I guess.
>
>
Arrays in Ruby get unbearably slow when you get into the millions, it would
definitely be worth considering alternative implementations at that point.

require 'benchmark'

def remove_1millionth_index(old_ary)
  new_ary = old_ary.clone
  new_ary.delete_at 1_000_000
  new_ary
end

ary = Array(1..4_000_000)

Benchmark.bm do |b|
  b.report {
    100.times do
      remove_1millionth_index ary
    end
  }
end


# =================  output  =================
#        user     system      total        real
#  7.875000   0.938000   8.813000 (  8.984000)