module RubyShogi::Bench

Public Class Methods

caps() click to toggle source
# File lib/ruby_shogi/bench.rb, line 83
def Bench.caps
        header("caps")
        Benchmark.bm(10) do |x|
                x.report(".upcase") { f6 }
                x.report("&:upcase") { f7 }
        end
end
f1() click to toggle source
# File lib/ruby_shogi/bench.rb, line 12
def Bench.f1
        n = 0
        1000_000.times { |i| n += (i%80) }
        n
end
f10() click to toggle source
# File lib/ruby_shogi/bench.rb, line 101
def Bench.f10
        s, s2 = "a", "abc"
        20_000.times { s = "#{s}#{s2}" }
end
f2() click to toggle source
# File lib/ruby_shogi/bench.rb, line 18
def Bench.f2
        n = 0
        1000_000.times { |i| n += (i%80) }
        n
end
f3() click to toggle source
# File lib/ruby_shogi/bench.rb, line 24
def Bench.f3
        n, i = 0, 0
        while i < 1000_000 
                n, i = n + (i%80), i + 1
        end
        n
end
f3_1() click to toggle source
# File lib/ruby_shogi/bench.rb, line 32
def Bench.f3_1
        n = 0
        for i in 0..1000_000 do
                n = n + (i%80)
        end
        n
end
f4() click to toggle source
# File lib/ruby_shogi/bench.rb, line 51
def Bench.f4
        n = 0
        1000_000.times { |i| n += i%42 }
        n
end
f5() click to toggle source
# File lib/ruby_shogi/bench.rb, line 57
def Bench.f5
        n = 0
        1000_000.times { |i| n += i.modulo 42 }
        n
end
f6() click to toggle source
# File lib/ruby_shogi/bench.rb, line 71
def Bench.f6
        s, caps = ["abc", "def", "ghi", "jkl"], ""
        100_000.times { caps = s.map { |str| str.upcase } }
        caps
end
f7() click to toggle source
# File lib/ruby_shogi/bench.rb, line 77
def Bench.f7
        s, caps = ["abc", "def", "ghi", "jkl"], ""
        100_000.times { caps = s.map(&:upcase) }
        caps
end
f8() click to toggle source
# File lib/ruby_shogi/bench.rb, line 91
def Bench.f8
        s, s2 = "a", "abc"
        20_000.times { s += s2 }
end
f9() click to toggle source
# File lib/ruby_shogi/bench.rb, line 96
def Bench.f9
        s, s2 = "a", "abc"
        20_000.times { s << s2 }
end
go() click to toggle source
# File lib/ruby_shogi/bench.rb, line 119
def Bench.go
        loops
        modulo
        caps
        strings
end
header(msg) click to toggle source
# File lib/ruby_shogi/bench.rb, line 106
def Bench.header(msg)
        puts "... #{msg} ..."
end
loops() click to toggle source
# File lib/ruby_shogi/bench.rb, line 40
def Bench.loops
        header("loops")
        Benchmark.bm(10) do |x|
                x.report("each") { f1 }
                x.report("times") { f2 }
                x.report("while") { f3 }
                x.report("for") { f3_1 }
                #x.compare!
        end
end
modulo() click to toggle source
# File lib/ruby_shogi/bench.rb, line 63
def Bench.modulo
        header("modulo")
        Benchmark.bm(10) do |x|
                x.report("%") { f4 }
                x.report("modulo") { f5 }
        end
end
strings() click to toggle source
# File lib/ruby_shogi/bench.rb, line 110
def Bench.strings
        header("strings")
        Benchmark.bm(10) do |x|
                x.report("+=") { f8 }
                x.report("<<") { f9 }
                x.report("\#\{\}") { f10 }
        end
end