Skip to content

Instantly share code, notes, and snippets.

@jgaskins
Created April 11, 2013 17:25
Show Gist options
  • Save jgaskins/5365357 to your computer and use it in GitHub Desktop.
Save jgaskins/5365357 to your computer and use it in GitHub Desktop.
Effects of Module#extend busting method caches on MRI 1.9.3, Rubinius and JRuby.
require 'benchmark'
class C
def m
end
end
class D
def m
end
end
module H
end
Benchmark.bmbm do |x|
iterations = 1_000_000
x.report 'no extending' do
iterations.times do
l = C.new
j = C.new
l.m; l.m; l.m; l.m;l.m; l.m; l.m; l.m;
end
end
x.report 'extending different object' do
iterations.times do
l = C.new
j = C.new
j.extend H
l.m; l.m; l.m; l.m;l.m; l.m; l.m; l.m;
end
end
x.report 'extending same object' do
iterations.times do
l = C.new
j = C.new
j.extend H
j.m; j.m; j.m; j.m;j.m; j.m; j.m; j.m;
end
end
x.report 'extending object of another class' do
iterations.times do
l = C.new
j = D.new
j.extend H
l.m; l.m; l.m; l.m;l.m; l.m; l.m; l.m;
end
end
end

MRI 1.9.3

ruby 1.9.3p286 (2012-10-12 revision 37165) [x86_64-darwin12.2.0]
malloc_limit=1000000000 (8000000)
heap_min_slots=1000000 (10000)
Rehearsal ---------------------------------------------------------------------
no extending                        3.660000   0.000000   3.660000 (  3.688392)
extending different object          7.760000   0.090000   7.850000 (  7.900708)
extending same object               8.010000   0.050000   8.060000 (  8.113058)
extending object of another class   7.780000   0.060000   7.840000 (  8.334709)
----------------------------------------------------------- total: 27.410000sec

                                        user     system      total        real
no extending                        3.650000   0.010000   3.660000 (  3.772231)
extending different object          7.610000   0.050000   7.660000 (  7.734326)
extending same object               7.930000   0.050000   7.980000 (  8.044671)
extending object of another class   7.640000   0.050000   7.690000 (  7.735318)

Rubinius

rubinius 2.0.0.rc1 (1.9.3 ca652815 yyyy-mm-dd JI) [x86_64-apple-darwin12.2.0]
Rehearsal ---------------------------------------------------------------------
no extending                        0.440685   0.011284   0.451969 (  0.393969)
extending different object         22.285687   0.904542  23.190229 ( 23.464282)
extending same object              22.138033   0.881234  23.019267 ( 23.861670)
extending object of another class  20.692522   0.746317  21.438839 ( 21.529753)
----------------------------------------------------------- total: 68.100304sec

                                        user     system      total        real
no extending                        0.172294   0.001502   0.173796 (  0.174448)
extending different object         20.782337   0.687167  21.469504 ( 22.307213)
extending same object              22.078339   0.731828  22.810167 ( 23.279143)
extending object of another class  20.613544   0.666262  21.279806 ( 21.480021)

JRuby

jruby 1.7.3 (1.9.3p385) 2013-02-21 dac429b on Java HotSpot(TM) 64-Bit Server VM 1.7.0_15-b03 [darwin-x86_64]
Rehearsal ---------------------------------------------------------------------
no extending                        1.270000   0.020000   1.290000 (  0.812000)
extending different object          9.760000   0.290000  10.050000 (  7.414000)
extending same object               9.810000   0.300000  10.110000 (  7.904000)
extending object of another class   8.560000   0.270000   8.830000 (  6.652000)
----------------------------------------------------------- total: 30.280000sec

                                        user     system      total        real
no extending                        0.300000   0.010000   0.310000 (  0.313000)
extending different object          8.830000   0.250000   9.080000 (  6.837000)
extending same object               9.340000   0.280000   9.620000 (  7.512000)
extending object of another class   8.140000   0.260000   8.400000 (  6.372000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment