Skip to content

Instantly share code, notes, and snippets.

@sbellware
Created December 20, 2011 03:18
Show Gist options
  • Save sbellware/1500079 to your computer and use it in GitHub Desktop.
Save sbellware/1500079 to your computer and use it in GitHub Desktop.
Extend vs Include in Metaclass
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 C
def metaclass; class << self; self; end; end
def meta_eval &blk; metaclass.instance_eval &blk; end
def meta_include(mod)
meta_eval { include mod }
end
end
t1 = Time.now
n.times do
c = C.new
c.meta_include M
end
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