Skip to content

Commit

Permalink
Optimize the rary_insert().
Browse files Browse the repository at this point in the history
* before
                               user     system      total        real
filled object / string     0.990000   0.000000   0.990000 (  0.981791)
filled object / numeric    0.930000   0.000000   0.930000 (  0.928791)
empty array / string       0.160000   0.010000   0.170000 (  0.095161)
empty array / numeric      0.010000   0.000000   0.010000 (  0.015316)

* after
                               user     system      total        real
filled object / string     0.060000   0.000000   0.060000 (  0.057018)
filled object / numeric    0.050000   0.000000   0.050000 (  0.051273)
empty array / string       0.080000   0.000000   0.080000 (  0.045073)
empty array / numeric      0.020000   0.000000   0.020000 (  0.014007)

{{{
require 'benchmark'

Benchmark.bm(25) do |x|

  ary  = ['obj'] * 10000
  x.report "filled object / string" do
    1000.times do
      ary.unshift("a")
    end
  end

  ary  = ['obj'] * 10000
  x.report "filled object / numeric" do
    1000.times do
      ary.unshift(0)
    end
  end

  ary = Array.new(10000)
  x.report "empty array / string" do
    1000.times do
      ary.unshift("a")
    end
  end

  ary = Array.new(10000)
  x.report "empty array / numeric" do
    1000.times do
      ary.unshift(0)
    end
  end

end
}}}
  • Loading branch information
Watson1978 committed Jun 19, 2011
1 parent 94a97d3 commit a621583
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions array.c
Expand Up @@ -390,9 +390,9 @@ rary_insert(VALUE ary, long idx, VALUE val)
}
else if (idx < RARY(ary)->len) {
rary_reserve(ary, RARY(ary)->len + 1);
for (size_t i = RARY(ary)->len; i > idx; i--) {
rary_elt_set(ary, i, rary_elt(ary, i - 1));
}
GC_MEMMOVE(&RARY(ary)->elements[RARY(ary)->beg + idx + 1],
&RARY(ary)->elements[RARY(ary)->beg + idx],
sizeof(VALUE) * (RARY(ary)->len - idx));
rary_elt_set(ary, idx, val);
RARY(ary)->len++;
}
Expand Down

0 comments on commit a621583

Please sign in to comment.