Skip to content

Commit

Permalink
Optimize Array.new with block.
Browse files Browse the repository at this point in the history
* before
                   user     system      total        real
without block  0.280000   0.060000   0.340000 (  0.185518)
with block     0.680000   0.120000   0.800000 (  0.517797)

* after
                   user     system      total        real
without block  0.290000   0.060000   0.350000 (  0.186769)
with block     0.490000   0.040000   0.530000 (  0.394732)

{{{
require 'benchmark'

Benchmark.bm(13) do |x|
  x.report "without block" do
    1000.times do
      Array.new(10000)
    end
  end

  x.report "with block" do
    1000.times do
      Array.new(10000) {|i| }
    end
  end
end
}}}
  • Loading branch information
Watson1978 committed Jun 18, 2011
1 parent 454916f commit 94a97d3
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion array.c
Expand Up @@ -330,6 +330,7 @@ rary_initialize(VALUE ary, SEL sel, int argc, VALUE *argv)

const long len = NUM2LONG(size);
assert_ary_len(len);
rary_resize(ary, len);
if (rb_block_given_p()) {
if (argc == 2) {
rb_warn("block supersedes default value argument");
Expand All @@ -344,7 +345,6 @@ rary_initialize(VALUE ary, SEL sel, int argc, VALUE *argv)
}
}
else {
rary_resize(ary, len);
for (long i = 0; i < len; i++) {
rary_store(ary, i, val);
}
Expand Down

0 comments on commit 94a97d3

Please sign in to comment.