Blogged by Ujihisa. Standard methods of programming and thoughts including Clojure, Vim, LLVM, Haskell, Ruby and Mathematics written by a Japanese programmer. github/ujihisa

Saturday, May 2, 2009

A benchmark of Array#to_s

A bugfix patch may make Array#to_s slow. I am finding out how slow it becomes.

That patch changes the definition of Array#to_s from:

rb_define_method(rb_cArray, "to_s", rb_ary_inspect, 0);
rb_define_method(rb_cArray, "inspect", rb_ary_inspect, 0);

to:

rb_define_method(rb_cArray, "inspect", rb_ary_inspect, 0);
rb_define_alias(rb_cArray,  "to_s", "inspect");

This change effects only in the array has itself recursively.

Benchmark code I use is:

require 'benchmark'
short_a = Array.new(100) { rand }
long_a = Array.new(10000) { rand }

Benchmark.bmbm do |b|
  b.report do
    1000.times do
      short_a.to_s
    end
  end

  b.report do
    10.times do
      long_a.to_s
    end
  end
end

And the results are below.

Conventional (before the patch applied)

Rehearsal ------------------------------------
   0.810000   0.010000   0.820000 (  0.836625)
   0.870000   0.010000   0.880000 (  0.901661)
--------------------------- total: 1.700000sec

       user     system      total        real
   0.740000   0.010000   0.750000 (  0.748501)
   0.770000   0.010000   0.780000 (  0.801869)

Current (after the patch applied)

Rehearsal ------------------------------------
   0.820000   0.000000   0.820000 (  0.852360)
   0.860000   0.010000   0.870000 (  0.897602)
--------------------------- total: 1.690000sec

       user     system      total        real
   0.850000   0.010000   0.860000 (  0.873125)
   0.830000   0.010000   0.840000 (  0.871903)

There are certainly slow changes, but they seem enough slight. What do you think about it?

No comments:

Post a Comment

Followers