Skip to content

Instantly share code, notes, and snippets.

@deepthawtz
Created June 8, 2011 04:09
Show Gist options
  • Save deepthawtz/1013758 to your computer and use it in GitHub Desktop.
Save deepthawtz/1013758 to your computer and use it in GitHub Desktop.
require "benchmark"
N = 300_000
Benchmark.bmbm do |x|
x.report("define_method") do
class Foo
(1..N).each do |i|
define_method "method_#{i}".to_sym do
"bla_#{i}"
end
end
end
end
x.report("class_eval ") do
class Bar
(1..N).each do |i|
class_eval <<-METHOD
def method_#{i}
"blah_#{i}"
end
METHOD
end
end
end
end
__END__
Rehearsal -------------------------------------------------
define_method 5.830000 0.240000 6.070000 ( 6.081396)
class_eval 5.300000 0.100000 5.400000 ( 5.396433)
--------------------------------------- total: 11.470000sec
user system total real
define_method 15.240000 0.020000 15.260000 ( 15.253883)
class_eval 5.680000 0.010000 5.690000 ( 5.680143)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment