Skip to content

Commit

Permalink
Optimize the ary_shared_first().
Browse files Browse the repository at this point in the history
* before
                user     system      total        real
shift       1.410000   0.020000   1.430000 (  0.778395)
shift(10)   0.910000   0.040000   0.950000 (  0.718543)
shift(100)  1.830000   0.090000   1.920000 (  1.379111)

* after
                user     system      total        real
shift       1.410000   0.030000   1.440000 (  0.778768)
shift(10)   0.970000   0.030000   1.000000 (  0.720666)
shift(100)  1.190000   0.080000   1.270000 (  0.890673)

{{{
require 'benchmark'

Benchmark.bm(10) do |x|

  x.report "shift" do
    50.times do
      ary  = ['obj'] * 100000
      1000.times do
        ary.shift
      end
    end
  end

  x.report "shift(10)" do
    50.times do
      ary  = ['obj'] * 100000
      1000.times do
        ary.shift(10)
      end
    end
  end

  x.report "shift(100)" do
    50.times do
      ary  = ['obj'] * 100000
      1000.times do
        ary.shift(100)
      end
    end
  end

end
}}}
  • Loading branch information
Watson1978 committed Jun 19, 2011
1 parent a621583 commit c890444
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions array.c
Expand Up @@ -422,10 +422,11 @@ ary_shared_first(int argc, VALUE *argv, VALUE ary, bool last, bool remove)
}

VALUE result = rb_ary_new();
for (long i = 0; i < n; i++) {
VALUE item = rary_elt(ary, i + offset);
rary_push(result, item);
}
rary_reserve(result, n);
GC_MEMMOVE(rary_ptr(result),
&RARY(ary)->elements[RARY(ary)->beg + offset],
sizeof(VALUE) * n);
RARY(result)->len = n;
if (remove) {
for (long i = 0; i < n; i++) {
rary_erase(ary, offset, 1);
Expand Down

0 comments on commit c890444

Please sign in to comment.