Skip to content

Instantly share code, notes, and snippets.

@botanicus
Forked from matthewrudy/method_definition.rb
Created August 28, 2009 19:27
Show Gist options
  • Save botanicus/177171 to your computer and use it in GitHub Desktop.
Save botanicus/177171 to your computer and use it in GitHub Desktop.
# ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-darwin9.6.0]
user system total real
by_define_method 0.680000 0.000000 0.680000 ( 0.700586)
by_explicit_definition 0.560000 0.010000 0.570000 ( 0.581977)
by_eval 0.550000 0.000000 0.550000 ( 0.553484)
by_class_eval 0.540000 0.010000 0.550000 ( 0.561800)
by_class_eval_block 0.540000 0.000000 0.540000 ( 0.548041)
by_module_eval 0.550000 0.010000 0.560000 ( 0.560635)
by_module_eval_block 0.520000 0.000000 0.520000 ( 0.539168)
# Matthew, which Ruby version do you have?
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)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment