Skip to content

Instantly share code, notes, and snippets.

@jacaetevha
Forked from anonymous/output
Created December 17, 2012 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacaetevha/4318843 to your computer and use it in GitHub Desktop.
Save jacaetevha/4318843 to your computer and use it in GitHub Desktop.
RE: http://eval.in/4817 ... Singleton methods add some cost of execution to objects in MRI 1.9.3, not much in MRI 1.8.7, and JRuby doesn't seem to be affected. Note: the original anonymous Gist was mine also (didn't realize I wasn't logged in).
~/.rvm/rubies/jruby-1.6.5/bin/ruby
before singleton method definition
0.991000 0.000000 0.991000 ( 0.990000)
after singleton method definition
0.641000 0.000000 0.641000 ( 0.641000)
~/.rvm/rubies/jruby-1.6.7.2/bin/ruby
before singleton method definition
0.554000 0.000000 0.554000 ( 0.555000)
after singleton method definition
0.502000 0.000000 0.502000 ( 0.502000)
~/.rvm/rubies/jruby-1.6.8/bin/ruby
before singleton method definition
0.530000 0.000000 0.530000 ( 0.530000)
after singleton method definition
0.510000 0.000000 0.510000 ( 0.510000)
~/.rvm/rubies/jruby-1.7.0/bin/ruby
before singleton method definition
1.730000 0.040000 1.770000 ( 0.859000)
after singleton method definition
0.700000 0.000000 0.700000 ( 0.518000)
~/.rvm/rubies/jruby-1.7.1/bin/ruby
before singleton method definition
1.610000 0.040000 1.650000 ( 0.779000)
after singleton method definition
0.760000 0.000000 0.760000 ( 0.578000)
~/.rvm/rubies/jruby-head/bin/ruby
before singleton method definition
1.540000 0.050000 1.590000 ( 0.775000)
after singleton method definition
0.710000 0.010000 0.720000 ( 0.526000)
~/.rvm/rubies/ree-1.8.7-2012.02/bin/ruby
before singleton method definition
0.830000 0.000000 0.830000 ( 0.828132)
after singleton method definition
0.800000 0.000000 0.800000 ( 0.806208)
~/.rvm/rubies/ruby-1.8.7-p352/bin/ruby
before singleton method definition
0.930000 0.000000 0.930000 ( 0.930660)
after singleton method definition
0.940000 0.000000 0.940000 ( 0.942419)
~/.rvm/rubies/ruby-1.8.7-p358/bin/ruby
before singleton method definition
0.960000 0.000000 0.960000 ( 0.962650)
after singleton method definition
0.950000 0.000000 0.950000 ( 0.954485)
~/.rvm/rubies/ruby-1.8.7-p370/bin/ruby
before singleton method definition
0.960000 0.000000 0.960000 ( 0.963616)
after singleton method definition
0.950000 0.000000 0.950000 ( 0.947487)
~/.rvm/rubies/ruby-1.9.3-p194/bin/ruby
before singleton method definition
0.160000 0.000000 0.160000 ( 0.166036)
after singleton method definition
0.410000 0.000000 0.410000 ( 0.414524)
~/.rvm/rubies/ruby-1.9.3-p286/bin/ruby
before singleton method definition
0.390000 0.000000 0.390000 ( 0.392831)
after singleton method definition
1.050000 0.000000 1.050000 ( 1.050109)
~/.rvm/rubies/ruby-1.9.3-p327/bin/ruby
before singleton method definition
0.180000 0.000000 0.180000 ( 0.185082)
after singleton method definition
0.410000 0.000000 0.410000 ( 0.401608)
require "benchmark"
include Benchmark
puts "\n\n#{`which ruby`.chomp}"
GC.disable unless RUBY_PLATFORM =~ /java/
upper_boundary = ARGV[0].to_i || 100
TIMES = 1_000
eval "def run; 10_000.times { #{"$a[5]\n" * TIMES} } end"
$a = (1..upper_boundary).to_a
puts "before singleton method definition #{$a.size} elements"
puts measure { run }
def $a.x; end
puts "after singleton method definition #{$a.size} elements"
puts measure { run }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment