Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'benchmark'
- #
- # Benchmarks for various miscellaneous operations.
- #
- # This set of benchmarks looks at two ways of constructing a logger:
- # with an interpolated string that gets operated on given a flag,
- # or a block that yields the interpolated string, given a flag.
- #
- # user system total real
- # ** short strings **
- # m("string", true) 0.100000 0.000000 0.100000 ( 0.104513)
- # m("string", false) 0.100000 0.000000 0.100000 ( 0.102459)
- # m(true) # :yield: 0.170000 0.000000 0.170000 ( 0.161769)
- # m(false) 0.040000 0.000000 0.040000 ( 0.042795)
- #
- # ** long strings **
- # m("string", true) 0.270000 0.000000 0.270000 ( 0.273281)
- # m("string", false) 0.270000 0.000000 0.270000 ( 0.276199)
- # m(true) # :yield: 0.320000 0.000000 0.320000 ( 0.316087)
- # m(false) 0.040000 0.000000 0.040000 ( 0.044935)
- #
- # Quite interesting. Interpolation up front is a kind of hedge, while
- # the block is either faster or slower for short strings. Long strings
- # clearly benefit the block
- #
- # ** assignment from array **
- # a, b = *array 0.080000 0.000000 0.080000 ( 0.080729)
- # a, *b = array 0.110000 0.000000 0.110000 ( 0.112028)
- # a, b = (shift, shift) 0.140000 0.000000 0.140000 ( 0.138396)
- # a = (shift); b = (shift) 0.100000 0.000000 0.100000 ( 0.099711)
- #
- # Very interesting. Inline assignments using splat are very fast (and)
- # incidentally MUCH faster on ruby 1.9. Shifts are not preferable.
- #
- # ** various operations **
- # !!obj 0.170000 0.000000 0.170000 ( 0.182985)
- # obj ? true : false 0.150000 0.000000 0.150000 ( 0.156731)
- # if obj then else end 0.160000 0.000000 0.160000 ( 0.161405)
- #
- # Demonstrates the full syntax is slightly faster.
- #
- # ** method signatures **
- # method call 0.300000 0.000000 0.300000 ( 0.300031)
- # method with block 0.360000 0.000000 0.360000 ( 0.370484)
- # method with &block 0.440000 0.000000 0.440000 ( 0.438172)
- # bloc call 0.330000 0.000000 0.330000 ( 0.328342)
- # bloc with block 2.790000 0.470000 3.260000 ( 3.272963)
- # bloc with &block 0.450000 0.000000 0.450000 ( 0.453583)
- #
- # Note it is very expensive to turn a block into a proc
- Benchmark.bm(25) do |x|
- puts "** short strings **"
- n = 100000
- str = "interpolation"
- def m_str(input, check)
- check ? true : false
- end
- x.report "m(\"string\", true)" do
- n.times { m_str("string #{str}", true) }
- end
- x.report "m(\"string\", false)" do
- n.times { m_str("string #{str}", false) }
- end
- def m_yield(check)
- check ? yield : false
- end
- x.report "m(true) # :yield: " do
- n.times { m_yield(true) { "string #{str}" } }
- end
- x.report "m(false)" do
- n.times { m_yield(false) { "string #{str}" } }
- end
- puts
- puts "** long strings **"
- x.report "m(\"string\", true)" do
- n.times { m_str("string #{str} string #{str} string #{str} string #{str} string #{str}", true) }
- end
- x.report "m(\"string\", false)" do
- n.times { m_str("string #{str} string #{str} string #{str} string #{str} string #{str}", false) }
- end
- x.report "m(true) # :yield: " do
- n.times { m_yield(true) { "string #{str} string #{str} string #{str} string #{str} string #{str}" } }
- end
- x.report "m(false)" do
- n.times { m_yield(false) { "string #{str} string #{str} string #{str} string #{str} string #{str}" } }
- end
- puts
- puts "** assignment from array **"
- x.report "a, b = *array" do
- n.times {
- array = [1,2,3]
- a, b = *array
- }
- end
- x.report "a, *b = array" do
- n.times {
- array = [1,2,3]
- a, *b = array
- }
- end
- x.report "a, b = (shift, shift)" do
- n.times {
- array = [1,2,3]
- a, b = array.shift, array.shift
- }
- end
- x.report "a = (shift); b = (shift)" do
- n.times {
- array = [1,2,3]
- a = array.shift
- b = array.shift
- }
- end
- x.report "a, b, c, d, e = *array" do
- n.times {
- array = [1,2,3,4,5]
- a, b, c, d, e = *array
- }
- end
- x.report "a = (shift); ..." do
- n.times {
- array = [1,2,3,4,5]
- a = array.shift
- b = array.shift
- c = array.shift
- d = array.shift
- e = array.shift
- }
- end
- puts
- puts "** various operations **"
- x.report "!!obj" do
- obj = Object.new
- (10 * n).times { !!obj }
- end
- x.report "obj ? true : false" do
- obj = Object.new
- (10 * n).times { obj ? true : false }
- end
- x.report "if obj then else end" do
- obj = Object.new
- (10 * n).times { if obj then true else false end }
- end
- puts
- puts "** method signatures **"
- class Sample
- def meth
- end
- def bloc(&block)
- end
- end
- x.report "method call" do
- obj = Sample.new
- (10 * n).times { obj.meth }
- end
- x.report "method with block" do
- obj = Sample.new
- (10 * n).times { obj.meth {} }
- end
- x.report "method with &block" do
- obj = Sample.new
- block = lambda {}
- (10 * n).times { obj.meth(&block) }
- end
- x.report "bloc call" do
- obj = Sample.new
- (10 * n).times { obj.bloc }
- end
- x.report "bloc with block" do
- obj = Sample.new
- (10 * n).times { obj.bloc {} }
- end
- x.report "bloc with &block" do
- obj = Sample.new
- block = lambda {}
- (10 * n).times { obj.bloc(&block) }
- end
- end
Add Comment
Please, Sign In to add comment