module Shuriken::Bench

Public Class Methods

caps() click to toggle source
# File lib/shuriken/bench.rb, line 93
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/shuriken/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/shuriken/bench.rb, line 66
def Bench.f10
        s, s2 = "a", "abc"
        20_000.times { s = "#{s}#{s2}" }
end
f2() click to toggle source
# File lib/shuriken/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/shuriken/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
f4() click to toggle source
# File lib/shuriken/bench.rb, line 32
def Bench.f4
        n = 0
        1000_000.times { |i| n += i%42 }
        n
end
f5() click to toggle source
# File lib/shuriken/bench.rb, line 38
def Bench.f5
        n = 0
        1000_000.times { |i| n += i.modulo 42 }
        n
end
f6() click to toggle source
# File lib/shuriken/bench.rb, line 44
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/shuriken/bench.rb, line 50
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/shuriken/bench.rb, line 56
def Bench.f8
        s, s2 = "a", "abc"
        20_000.times { s += s2 }
end
f9() click to toggle source
# File lib/shuriken/bench.rb, line 61
def Bench.f9
        s, s2 = "a", "abc"
        20_000.times { s << s2 }
end
go() click to toggle source
# File lib/shuriken/bench.rb, line 110
def Bench.go
        loops
        modulo
        caps
        strings
end
header(msg) click to toggle source
# File lib/shuriken/bench.rb, line 71
def Bench.header(msg)
        puts "... #{msg} ..."
end
loops() click to toggle source
# File lib/shuriken/bench.rb, line 75
def Bench.loops
        header("loops")
        Benchmark.bm(10) do |x|
                x.report("each") { f1 }
                x.report("times") { f2 }
                x.report("while") { f3 }
                #x.compare!
        end
end
modulo() click to toggle source
# File lib/shuriken/bench.rb, line 85
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/shuriken/bench.rb, line 101
def Bench.strings
        header("strings")
        Benchmark.bm(10) do |x|
                x.report("+=") { f8 }
                x.report("<<") { f9 }
                x.report("\#\{\}") { f10 }
        end
end