Skip to content

Instantly share code, notes, and snippets.

@flazz
Forked from sbellware/gist:1500079
Created December 20, 2011 07:45
Show Gist options
  • Save flazz/1500686 to your computer and use it in GitHub Desktop.
Save flazz/1500686 to your computer and use it in GitHub Desktop.
Extend vs Include in Metaclass
require 'ruby-prof'
module M
def foo
puts "M#foo"
end
end
class C
end
n = 150000
t1 = Time.now
n.times do
c = C.new
c.extend M
end
t2 = Time.now
extend_time = t2 - t1
puts extend_time
class C2
end
RubyProf.start
t1 = Time.now
n.times do
c = C2.new
mc = c.instance_eval { class << self; self; end }
mc.instance_eval { include M }
end
result = RubyProf.stop
printer = RubyProf::GraphHtmlPrinter.new(result)
open('metaclass2.html', 'w'){ |io| printer.print io, :min_percent => 0 }
t2 = Time.now
meta_include_time = t2 - t1
puts meta_include_time
puts meta_include_time / extend_time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment