-
-
Save flazz/1500686 to your computer and use it in GitHub Desktop.
Extend vs Include in Metaclass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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