Skip to content

Instantly share code, notes, and snippets.

@takkanm
Created June 30, 2016 08:48
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 takkanm/4eef2970faf168c47f593ffa999d789e to your computer and use it in GitHub Desktop.
Save takkanm/4eef2970faf168c47f593ffa999d789e to your computer and use it in GitHub Desktop.
require 'benchmark'
module Foo
def foo;end
end
times = 1_000_000
Benchmark.bm 30 do |r|
r.report 'none' do
times.times do
o = Object.new
end
end
GC.start
r.report 'touch singleton_class' do
times.times do
o = Object.new
o.singleton_class
end
end
GC.start
r.report 'lookup' do
times.times do
o = Object.new
if o.singleton_class.ancestors.include? :hi
end
end
end
GC.start
r.report 'extend' do
times.times do
o = Object.new
o.extend Foo
end
end
GC.start
r.report 'singleton_class include' do
times.times do
o = Object.new
o.singleton_class.include Foo
end
end
GC.start
r.report 'singleton_class prepend' do
times.times do
o = Object.new
o.singleton_class.include Foo
end
end
end
user system total real
none 0.140000 0.000000 0.140000 ( 0.139669)
touch singleton_class 1.180000 0.040000 1.220000 ( 1.228629)
lookup 1.700000 0.060000 1.760000 ( 1.788575)
extend 2.090000 0.050000 2.140000 ( 2.155467)
singleton_class include 2.090000 0.050000 2.140000 ( 2.142550)
singleton_class prepend 1.960000 0.020000 1.980000 ( 1.990201)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment