Skip to content

Instantly share code, notes, and snippets.

@matthewrudy
Created August 28, 2009 16:35
Show Gist options
  • Save matthewrudy/177083 to your computer and use it in GitHub Desktop.
Save matthewrudy/177083 to your computer and use it in GitHub Desktop.
require 'benchmark'
class TheHost
define_method(:by_define_method) do
24*4
end
def by_explicit_definition
24*4
end
class_eval <<-EVAL
def by_class_eval
24*4
end
EVAL
class_eval do
def by_class_eval_block
24*4
end
end
module_eval <<-EVAL
def by_module_eval
24*4
end
EVAL
end
@@host = TheHost.new
TIMES = 1_000_000
Benchmark.bmbm do |x|
%w( by_define_method by_explicit_definition by_class_eval by_class_eval_block by_module_eval ).each do |meth|
x.report(meth) do
TIMES.times do
@@host.send(meth)
end
end
end
end
user system total real
by_define_method 1.760000 0.400000 2.160000 ( 2.158289)
by_explicit_definition 1.160000 0.240000 1.400000 ( 1.391353)
by_class_eval 1.100000 0.260000 1.360000 ( 1.370616)
by_class_eval_block 1.140000 0.240000 1.380000 ( 1.394569)
by_module_eval 1.150000 0.260000 1.410000 ( 1.429104)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment