Created
December 4, 2008 23:58
-
-
Save hassox/32156 to your computer and use it in GitHub Desktop.
This file contains 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 'benchmark' | |
class Foo | |
attr_accessor :the_blocks, :method_map | |
class Bar | |
def bar | |
:bar | |
end | |
end | |
def initialize(*labels, &block) | |
@the_blocks = [] | |
@method_map = Hash.new{|h,k| h[k] = []} | |
self.instance_eval(&block) | |
labels.each do |lab| | |
@the_blocks.each_with_index do |blk, index| | |
self.class.send(:define_method, :"made_up_#{lab}_#{index}", &blk) | |
@method_map[lab] << :"made_up_#{lab}_#{index}" | |
end | |
end | |
end | |
def blah | |
:blah | |
end | |
def execute_methods!(label) | |
@method_map[label].each{ |meth| self.send(meth)} | |
end | |
def execute_blocks!(label) | |
self.the_blocks.each do |blk| | |
blk.call | |
end | |
end | |
def execute_natural_methods!(label) | |
self.send(:blah) | |
end | |
def execute_subclass!(label) | |
Bar.new.send(:bar) | |
end | |
def execute_yielding_blocks!(label) | |
self.the_blocks.each do |blk| | |
block_executer(&blk) | |
end | |
end | |
def create_method(&block) | |
self.the_blocks << block | |
end | |
def block_executer | |
yield | |
end | |
end | |
@tester = Foo.new(:foo){ create_method { blah } } | |
n = 5000000 | |
Benchmark.bmbm do |r| | |
r.report("Natural Methods"){n.times{ @tester.execute_natural_methods!(:foo )}} | |
r.report("Subclass Methods"){n.times{ @tester.execute_subclass!(:foo )}} | |
r.report("Yielding Captured Blocks"){n.times{ @tester.execute_yielding_blocks!(:foo )}} | |
r.report("Block.call"){n.times{ @tester.execute_blocks!(:foo )}} | |
r.report("define_method methods"){n.times{ @tester.execute_methods!(:foo )}} | |
end | |
This file contains 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
Rehearsal ------------------------------------------------------------ | |
Natural Methods 7.570000 0.090000 7.660000 ( 8.440553) | |
Subclass Methods 10.070000 0.110000 10.180000 ( 11.195944) | |
Yielding Captured Blocks 13.890000 0.130000 14.020000 ( 14.970620) | |
Block.call 14.200000 0.130000 14.330000 ( 15.138450) | |
define_method methods 18.170000 0.170000 18.340000 ( 19.416476) | |
-------------------------------------------------- total: 64.530000sec | |
user system total real | |
Natural Methods 7.470000 0.070000 7.540000 ( 8.006188) | |
Subclass Methods 10.090000 0.100000 10.190000 ( 11.484167) | |
Yielding Captured Blocks 13.990000 0.140000 14.130000 ( 15.635487) | |
Block.call 14.320000 0.150000 14.470000 ( 15.743148) | |
define_method methods 18.450000 0.230000 18.680000 ( 20.740741) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment