Skip to content

Instantly share code, notes, and snippets.

@dscataglini
Created December 17, 2008 22:50
Show Gist options
  • Save dscataglini/37279 to your computer and use it in GitHub Desktop.
Save dscataglini/37279 to your computer and use it in GitHub Desktop.
T = 1_000_000
ARGS = []
OBJECTS = []
ALPHA = ('a'..'z').to_a
require 'benchmark'
class G0
def bar(*args)
1
end
end
class G1
def bar(a, *args)
1
end
end
class G2
def bar(a, b, *args)
1
end
end
class G3
def bar(a, b, c, *args)
1
end
end
class H
def bar(h)
1
end
end
def args(int)
(0..(int - 1)).to_a if int > 0
end
class B0
def bar();1;end
end
b0 = B0.new
ARGS << args(0)
class C0
def bar();1;end
end
c0 = C0.new
class D0
def bar( &block);1;end
end
d0 = D0.new
class B1
def bar(a);1;end
end
b1 = B1.new
ARGS << args(1)
class C1
def bar(a = 1);1;end
end
c1 = C1.new
class D1
def bar(a , &block);1;end
end
d1 = D1.new
class B2
def bar(a, b);1;end
end
b2 = B2.new
ARGS << args(2)
class C2
def bar(a = 1, b = 1);1;end
end
c2 = C2.new
class D2
def bar(a, b , &block);1;end
end
d2 = D2.new
class B3
def bar(a, b, c);1;end
end
b3 = B3.new
ARGS << args(3)
class C3
def bar(a = 1, b = 1, c = 1);1;end
end
c3 = C3.new
class D3
def bar(a, b, c , &block);1;end
end
d3 = D3.new
class B4
def bar(a, b, c, d);1;end
end
b4 = B4.new
ARGS << args(4)
class C4
def bar(a = 1, b = 1, c = 1, d = 1);1;end
end
c4 = C4.new
class D4
def bar(a, b, c, d , &block);1;end
end
d4 = D4.new
class B5
def bar(a, b, c, d, e);1;end
end
b5 = B5.new
ARGS << args(5)
class C5
def bar(a = 1, b = 1, c = 1, d = 1, e = 1);1;end
end
c5 = C5.new
class D5
def bar(a, b, c, d, e , &block);1;end
end
d5 = D5.new
class B6
def bar(a, b, c, d, e, f);1;end
end
b6 = B6.new
ARGS << args(6)
class C6
def bar(a = 1, b = 1, c = 1, d = 1, e = 1, f = 1);1;end
end
c6 = C6.new
class D6
def bar(a, b, c, d, e, f , &block);1;end
end
d6 = D6.new
class B7
def bar(a, b, c, d, e, f, g);1;end
end
b7 = B7.new
ARGS << args(7)
class C7
def bar(a = 1, b = 1, c = 1, d = 1, e = 1, f = 1, g = 1);1;end
end
c7 = C7.new
class D7
def bar(a, b, c, d, e, f, g , &block);1;end
end
d7 = D7.new
class B8
def bar(a, b, c, d, e, f, g, h);1;end
end
b8 = B8.new
ARGS << args(8)
class C8
def bar(a = 1, b = 1, c = 1, d = 1, e = 1, f = 1, g = 1, h = 1);1;end
end
c8 = C8.new
class D8
def bar(a, b, c, d, e, f, g, h , &block);1;end
end
d8 = D8.new
class B9
def bar(a, b, c, d, e, f, g, h, i);1;end
end
b9 = B9.new
ARGS << args(9)
class C9
def bar(a = 1, b = 1, c = 1, d = 1, e = 1, f = 1, g = 1, h = 1, i = 1);1;end
end
c9 = C9.new
class D9
def bar(a, b, c, d, e, f, g, h, i , &block);1;end
end
d9 = D9.new
class B10
def bar(a, b, c, d, e, f, g, h, i, j);1;end
end
b10 = B10.new
ARGS << args(10)
class C10
def bar(a = 1, b = 1, c = 1, d = 1, e = 1, f = 1, g = 1, h = 1, i = 1, j = 1);1;end
end
c10 = C10.new
class D10
def bar(a, b, c, d, e, f, g, h, i, j , &block);1;end
end
d10 = D10.new
Benchmark.bm(40) do |bm|
bm.report("call method with block not passed") { T.times { d1.bar 1} }
bm.report("call method with block not passed") { T.times { d2.bar 1, 2} }
bm.report("call method with block not passed") { T.times { d3.bar 1, 2, 3} }
bm.report("call method with block not passed") { T.times { d4.bar 1, 2, 3, 4} }
bm.report("call method with block not passed") { T.times { d5.bar 1, 2, 3, 4, 5} }
bm.report("call method with block not passed") { T.times { d6.bar 1, 2, 3, 4, 5, 6} }
bm.report("call method with block not passed") { T.times { d7.bar 1, 2, 3, 4, 5, 6, 7} }
bm.report("call method with block not passed") { T.times { d8.bar 1, 2, 3, 4, 5, 6, 7, 8} }
bm.report("call method with block not passed") { T.times { d9.bar 1, 2, 3, 4, 5, 6, 7, 8, 9} }
bm.report("call method with block not passed") { T.times { d10.bar 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }
end
Benchmark.bm(40) do |bm|
bm.report("call method with block passed") { T.times { d1.bar 1 do
1
end} }
bm.report("call method with block passed") { T.times { d2.bar 1, 2 do
1
end} }
bm.report("call method with block passed") { T.times { d3.bar 1, 2, 3 do
1
end} }
bm.report("call method with block passed") { T.times { d4.bar 1, 2, 3, 4 do
1
end} }
bm.report("call method with block passed") { T.times { d5.bar 1, 2, 3, 4, 5 do
1
end} }
bm.report("call method with block passed") { T.times { d6.bar 1, 2, 3, 4, 5, 6 do
1
end} }
bm.report("call method with block passed") { T.times { d7.bar 1, 2, 3, 4, 5, 6, 7 do
1
end} }
bm.report("call method with block passed") { T.times { d8.bar 1, 2, 3, 4, 5, 6, 7, 8 do
1
end} }
bm.report("call method with block passed") { T.times { d9.bar 1, 2, 3, 4, 5, 6, 7, 8, 9 do
1
end} }
bm.report("call method with block passed") { T.times { d10.bar 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 do
1
end} }
end
Benchmark.bm(40) do |bm|
bm.report("call method with no param defaults") { T.times { c1.bar 1} }
bm.report("call method with no param defaults") { T.times { c2.bar 1, 2} }
bm.report("call method with no param defaults") { T.times { c3.bar 1, 2, 3} }
bm.report("call method with no param defaults") { T.times { c4.bar 1, 2, 3, 4} }
bm.report("call method with no param defaults") { T.times { c5.bar 1, 2, 3, 4, 5} }
bm.report("call method with no param defaults") { T.times { c6.bar 1, 2, 3, 4, 5, 6} }
bm.report("call method with no param defaults") { T.times { c7.bar 1, 2, 3, 4, 5, 6, 7} }
bm.report("call method with no param defaults") { T.times { c8.bar 1, 2, 3, 4, 5, 6, 7, 8} }
bm.report("call method with no param defaults") { T.times { c9.bar 1, 2, 3, 4, 5, 6, 7, 8, 9} }
bm.report("call method with no param defaults") { T.times { c10.bar 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }
end
Benchmark.bm(40) do |bm|
bm.report("call method with 1 param defaults") { T.times { c1.bar } }
bm.report("call method with 1 param defaults") { T.times { c2.bar 1} }
bm.report("call method with 1 param defaults") { T.times { c3.bar 1, 2} }
bm.report("call method with 1 param defaults") { T.times { c4.bar 1, 2, 3} }
bm.report("call method with 1 param defaults") { T.times { c5.bar 1, 2, 3, 4} }
bm.report("call method with 1 param defaults") { T.times { c6.bar 1, 2, 3, 4, 5} }
bm.report("call method with 1 param defaults") { T.times { c7.bar 1, 2, 3, 4, 5, 6} }
bm.report("call method with 1 param defaults") { T.times { c8.bar 1, 2, 3, 4, 5, 6, 7} }
bm.report("call method with 1 param defaults") { T.times { c9.bar 1, 2, 3, 4, 5, 6, 7, 8} }
bm.report("call method with 1 param defaults") { T.times { c10.bar 1, 2, 3, 4, 5, 6, 7, 8, 9} }
end
Benchmark.bm(40) do |bm|
bm.report("call method with all param defaults") { T.times { c1.bar } }
bm.report("call method with all param defaults") { T.times { c2.bar } }
bm.report("call method with all param defaults") { T.times { c3.bar } }
bm.report("call method with all param defaults") { T.times { c4.bar } }
bm.report("call method with all param defaults") { T.times { c5.bar } }
bm.report("call method with all param defaults") { T.times { c6.bar } }
bm.report("call method with all param defaults") { T.times { c7.bar } }
bm.report("call method with all param defaults") { T.times { c8.bar } }
bm.report("call method with all param defaults") { T.times { c9.bar } }
bm.report("call method with all param defaults") { T.times { c10.bar } }
end
Benchmark.bm(40) do |bm|
bm.report("call method with 1 param") { T.times { b1.bar 1} }
bm.report("call method with 2 param") { T.times { b2.bar 1, 2} }
bm.report("call method with 3 param") { T.times { b3.bar 1, 2, 3} }
bm.report("call method with 4 param") { T.times { b4.bar 1, 2, 3, 4} }
bm.report("call method with 5 param") { T.times { b5.bar 1, 2, 3, 4, 5} }
bm.report("call method with 6 param") { T.times { b6.bar 1, 2, 3, 4, 5, 6} }
bm.report("call method with 7 param") { T.times { b7.bar 1, 2, 3, 4, 5, 6, 7} }
bm.report("call method with 8 param") { T.times { b8.bar 1, 2, 3, 4, 5, 6, 7, 8} }
bm.report("call method with 9 param") { T.times { b9.bar 1, 2, 3, 4, 5, 6, 7, 8, 9} }
bm.report("call method with 10 param") { T.times { b10.bar 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }
end
Benchmark.bm(40) do |bm|
bm.report("call method by send + 1 param") { T.times { b1.send :bar, 1} }
bm.report("call method by send + 2 param") { T.times { b2.send :bar, 1, 2} }
bm.report("call method by send + 3 param") { T.times { b3.send :bar, 1, 2, 3} }
bm.report("call method by send + 4 param") { T.times { b4.send :bar, 1, 2, 3, 4} }
bm.report("call method by send + 5 param") { T.times { b5.send :bar, 1, 2, 3, 4, 5} }
bm.report("call method by send + 6 param") { T.times { b6.send :bar, 1, 2, 3, 4, 5, 6} }
bm.report("call method by send + 7 param") { T.times { b7.send :bar, 1, 2, 3, 4, 5, 6, 7} }
bm.report("call method by send + 8 param") { T.times { b8.send :bar, 1, 2, 3, 4, 5, 6, 7, 8} }
bm.report("call method by send + 9 param") { T.times { b9.send :bar, 1, 2, 3, 4, 5, 6, 7, 8, 9} }
bm.report("call method by send + 10 param") { T.times { b10.send :bar, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }
end
Benchmark.bm(40) do |bm|
arg = (0..0).to_a
bm.report("*call method with 1 param with *") { T.times { b1.bar *arg } }
arg = (0..1).to_a
bm.report("*call method with 2 param with *") { T.times { b2.bar *arg } }
arg = (0..2).to_a
bm.report("*call method with 3 param with *") { T.times { b3.bar *arg } }
arg = (0..3).to_a
bm.report("*call method with 4 param with *") { T.times { b4.bar *arg } }
arg = (0..4).to_a
bm.report("*call method with 5 param with *") { T.times { b5.bar *arg } }
arg = (0..5).to_a
bm.report("*call method with 6 param with *") { T.times { b6.bar *arg } }
arg = (0..6).to_a
bm.report("*call method with 7 param with *") { T.times { b7.bar *arg } }
arg = (0..7).to_a
bm.report("*call method with 8 param with *") { T.times { b8.bar *arg } }
arg = (0..8).to_a
bm.report("*call method with 9 param with *") { T.times { b9.bar *arg } }
arg = (0..9).to_a
bm.report("*call method with 10 param with *") { T.times { b10.bar *arg } }
end
Benchmark.bm(40) do |bm|
obj = G0.new
arg = (0..1).to_a
bm.report("G0 called with 1 els in *") { T.times { obj.bar *arg } }
arg = (0..2).to_a
bm.report("G0 called with 2 els in *") { T.times { obj.bar *arg } }
arg = (0..3).to_a
bm.report("G0 called with 3 els in *") { T.times { obj.bar *arg } }
arg = (0..4).to_a
bm.report("G0 called with 4 els in *") { T.times { obj.bar *arg } }
arg = (0..5).to_a
bm.report("G0 called with 5 els in *") { T.times { obj.bar *arg } }
arg = (0..6).to_a
bm.report("G0 called with 6 els in *") { T.times { obj.bar *arg } }
arg = (0..7).to_a
bm.report("G0 called with 7 els in *") { T.times { obj.bar *arg } }
arg = (0..8).to_a
bm.report("G0 called with 8 els in *") { T.times { obj.bar *arg } }
arg = (0..9).to_a
bm.report("G0 called with 9 els in *") { T.times { obj.bar *arg } }
arg = (0..10).to_a
bm.report("G0 called with 10 els in *") { T.times { obj.bar *arg } }
end
Benchmark.bm(40) do |bm|
obj = G0.new
bm.report("G0 called with 1 els staticly") { T.times { obj.bar 1 }}
bm.report("G0 called with 2 els staticly") { T.times { obj.bar 1, 2 }}
bm.report("G0 called with 3 els staticly") { T.times { obj.bar 1, 2, 3 }}
bm.report("G0 called with 4 els staticly") { T.times { obj.bar 1, 2, 3, 4 }}
bm.report("G0 called with 5 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5 }}
bm.report("G0 called with 6 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6 }}
bm.report("G0 called with 7 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7 }}
bm.report("G0 called with 8 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8 }}
bm.report("G0 called with 9 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8, 9 }}
bm.report("G0 called with 10 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }}
end
Benchmark.bm(40) do |bm|
obj = G1.new
arg = (0..2).to_a
bm.report("G1 called with 1 els in *") { T.times { obj.bar *arg } }
arg = (0..3).to_a
bm.report("G1 called with 2 els in *") { T.times { obj.bar *arg } }
arg = (0..4).to_a
bm.report("G1 called with 3 els in *") { T.times { obj.bar *arg } }
arg = (0..5).to_a
bm.report("G1 called with 4 els in *") { T.times { obj.bar *arg } }
arg = (0..6).to_a
bm.report("G1 called with 5 els in *") { T.times { obj.bar *arg } }
arg = (0..7).to_a
bm.report("G1 called with 6 els in *") { T.times { obj.bar *arg } }
arg = (0..8).to_a
bm.report("G1 called with 7 els in *") { T.times { obj.bar *arg } }
arg = (0..9).to_a
bm.report("G1 called with 8 els in *") { T.times { obj.bar *arg } }
arg = (0..10).to_a
bm.report("G1 called with 9 els in *") { T.times { obj.bar *arg } }
arg = (0..11).to_a
bm.report("G1 called with 10 els in *") { T.times { obj.bar *arg } }
end
Benchmark.bm(40) do |bm|
obj = G1.new
bm.report("G1 called with 1 els staticly") { T.times { obj.bar 1, 2 }}
bm.report("G1 called with 2 els staticly") { T.times { obj.bar 1, 2, 3 }}
bm.report("G1 called with 3 els staticly") { T.times { obj.bar 1, 2, 3, 4 }}
bm.report("G1 called with 4 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5 }}
bm.report("G1 called with 5 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6 }}
bm.report("G1 called with 6 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7 }}
bm.report("G1 called with 7 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8 }}
bm.report("G1 called with 8 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8, 9 }}
bm.report("G1 called with 9 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }}
bm.report("G1 called with 10 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }}
end
Benchmark.bm(40) do |bm|
obj = G2.new
arg = (0..3).to_a
bm.report("G2 called with 1 els in *") { T.times { obj.bar *arg } }
arg = (0..4).to_a
bm.report("G2 called with 2 els in *") { T.times { obj.bar *arg } }
arg = (0..5).to_a
bm.report("G2 called with 3 els in *") { T.times { obj.bar *arg } }
arg = (0..6).to_a
bm.report("G2 called with 4 els in *") { T.times { obj.bar *arg } }
arg = (0..7).to_a
bm.report("G2 called with 5 els in *") { T.times { obj.bar *arg } }
arg = (0..8).to_a
bm.report("G2 called with 6 els in *") { T.times { obj.bar *arg } }
arg = (0..9).to_a
bm.report("G2 called with 7 els in *") { T.times { obj.bar *arg } }
arg = (0..10).to_a
bm.report("G2 called with 8 els in *") { T.times { obj.bar *arg } }
arg = (0..11).to_a
bm.report("G2 called with 9 els in *") { T.times { obj.bar *arg } }
arg = (0..12).to_a
bm.report("G2 called with 10 els in *") { T.times { obj.bar *arg } }
end
Benchmark.bm(40) do |bm|
obj = G2.new
bm.report("G2 called with 1 els staticly") { T.times { obj.bar 1, 2, 3 }}
bm.report("G2 called with 2 els staticly") { T.times { obj.bar 1, 2, 3, 4 }}
bm.report("G2 called with 3 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5 }}
bm.report("G2 called with 4 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6 }}
bm.report("G2 called with 5 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7 }}
bm.report("G2 called with 6 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8 }}
bm.report("G2 called with 7 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8, 9 }}
bm.report("G2 called with 8 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }}
bm.report("G2 called with 9 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }}
bm.report("G2 called with 10 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }}
end
Benchmark.bm(40) do |bm|
obj = G3.new
arg = (0..4).to_a
bm.report("G3 called with 1 els in *") { T.times { obj.bar *arg } }
arg = (0..5).to_a
bm.report("G3 called with 2 els in *") { T.times { obj.bar *arg } }
arg = (0..6).to_a
bm.report("G3 called with 3 els in *") { T.times { obj.bar *arg } }
arg = (0..7).to_a
bm.report("G3 called with 4 els in *") { T.times { obj.bar *arg } }
arg = (0..8).to_a
bm.report("G3 called with 5 els in *") { T.times { obj.bar *arg } }
arg = (0..9).to_a
bm.report("G3 called with 6 els in *") { T.times { obj.bar *arg } }
arg = (0..10).to_a
bm.report("G3 called with 7 els in *") { T.times { obj.bar *arg } }
arg = (0..11).to_a
bm.report("G3 called with 8 els in *") { T.times { obj.bar *arg } }
arg = (0..12).to_a
bm.report("G3 called with 9 els in *") { T.times { obj.bar *arg } }
arg = (0..13).to_a
bm.report("G3 called with 10 els in *") { T.times { obj.bar *arg } }
end
Benchmark.bm(40) do |bm|
obj = G3.new
bm.report("G3 called with 1 els staticly") { T.times { obj.bar 1, 2, 3, 4 }}
bm.report("G3 called with 2 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5 }}
bm.report("G3 called with 3 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6 }}
bm.report("G3 called with 4 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7 }}
bm.report("G3 called with 5 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8 }}
bm.report("G3 called with 6 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8, 9 }}
bm.report("G3 called with 7 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }}
bm.report("G3 called with 8 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }}
bm.report("G3 called with 9 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }}
bm.report("G3 called with 10 els staticly") { T.times { obj.bar 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }}
end
Benchmark.bm(40) do |bm|
obj = H.new
arg = ARGS[1].inject({}){|h, t| h[ALPHA[t]] = t; h}
bm.report("H called with hash with 1 els") { T.times { obj.bar arg }}
arg = ARGS[2].inject({}){|h, t| h[ALPHA[t]] = t; h}
bm.report("H called with hash with 2 els") { T.times { obj.bar arg }}
arg = ARGS[3].inject({}){|h, t| h[ALPHA[t]] = t; h}
bm.report("H called with hash with 3 els") { T.times { obj.bar arg }}
arg = ARGS[4].inject({}){|h, t| h[ALPHA[t]] = t; h}
bm.report("H called with hash with 4 els") { T.times { obj.bar arg }}
arg = ARGS[5].inject({}){|h, t| h[ALPHA[t]] = t; h}
bm.report("H called with hash with 5 els") { T.times { obj.bar arg }}
arg = ARGS[6].inject({}){|h, t| h[ALPHA[t]] = t; h}
bm.report("H called with hash with 6 els") { T.times { obj.bar arg }}
arg = ARGS[7].inject({}){|h, t| h[ALPHA[t]] = t; h}
bm.report("H called with hash with 7 els") { T.times { obj.bar arg }}
arg = ARGS[8].inject({}){|h, t| h[ALPHA[t]] = t; h}
bm.report("H called with hash with 8 els") { T.times { obj.bar arg }}
arg = ARGS[9].inject({}){|h, t| h[ALPHA[t]] = t; h}
bm.report("H called with hash with 9 els") { T.times { obj.bar arg }}
arg = ARGS[10].inject({}){|h, t| h[ALPHA[t]] = t; h}
bm.report("H called with hash with 10 els") { T.times { obj.bar arg }}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment