1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
require 'benchmark'
class MM
def method_missing(sym, *args)
1
end
end
class MM2
def method_missing(sym, *args)
send :bar
end
def bar
1
end
end
class MM2a
def method_missing(sym, *args)
send :bar1, *args
end
def bar1(a)
1
end
end
class MM2b
def method_missing(sym, *args)
send :bar4, *args
end
def bar4(a,b,c,d)
1
end
end
class MM3
def method_missing(sym, *args, &block)
1
end
end
mm = MM.new
mm2 = MM2.new
mm2a = MM2a.new
mm2b = MM2b.new
mm3 = MM3.new
5.times {
Benchmark.bm(40) do |bm|
bm.report("1M method_missing") do
1000000.times { mm.foo }
end
bm.report("1M sends") do
1000000.times { mm2.send :bar }
end
bm.report("1M method_missing with send") do
1000000.times { mm2.foo }
end
bm.report("1M method_missing with block") do
1000000.times { mm3.foo }
end
bm.report("1M 1-arg method_missing") do
1000000.times { mm.foo 1 }
end
bm.report("1M 1-arg sends") do
1000000.times { mm2a.send :bar1, 1 }
end
bm.report("1M 1-arg method_missing with send") do
1000000.times { mm2a.foo 1 }
end
bm.report("1M 1-arg method_missing with block") do
1000000.times { mm3.foo 1 }
end
bm.report("1M 4-arg method_missing") do
1000000.times { mm.foo 1,2,3,4 }
end
bm.report("1M 4-arg sends") do
1000000.times { mm2b.send :bar,1,2,3,4 }
end
bm.report("1M 4-arg method_missing with send") do
1000000.times { mm2b.foo 1,2,3,4 }
end
bm.report("1M 4-arg method_missing with block") do
1000000.times { mm3.foo 1,2,3,4 }
end
end
}
|