Created
March 8, 2014 02:37
-
-
Save kmdsbng/9424463 to your computer and use it in GitHub Desktop.
switchの書き方ごとにベンチマーク取ってみた結果
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
require 'benchmark' | |
class Character < Struct.new(:name, :level, :point) | |
def to_s | |
"%s:\tlv:%d\tpt:%d" % values | |
end | |
end | |
def bonus_point0(chara) | |
if chara.level.between?(1, 3) | |
chara.point += 10 | |
elsif chara.level.between?(4, 7) | |
chara.point += 5 | |
elsif chara.level.between?(8, 9) | |
chara.point += 3 | |
else | |
end | |
end | |
def bonus_point1(chara) | |
case | |
when chara.level.between?(1, 3) | |
chara.point += 10 | |
when chara.level.between?(4, 7) | |
chara.point += 5 | |
when chara.level.between?(8, 9) | |
chara.point += 3 | |
else | |
end | |
end | |
def bonus_point2(chara) | |
case chara.level | |
when 1, 2, 3 | |
chara.point += 10 | |
when 4, 5, 6, 7 | |
chara.point += 5 | |
when 8, 9 | |
chara.point += 3 | |
else | |
end | |
end | |
def bonus_point3(chara) | |
case chara.level | |
when 1..3 | |
chara.point += 10 | |
when 4..7 | |
chara.point += 5 | |
when 8, 9 | |
chara.point += 3 | |
else | |
end | |
end | |
def bonus_point4(chara) | |
low, mid, high = [1,2,3], [4,5,6,7], [8,9] | |
case chara.level | |
when *low | |
chara.point += 10 | |
when *mid | |
chara.point += 5 | |
when *high | |
chara.point += 3 | |
else | |
end | |
end | |
def bonus_point5(chara) | |
low, mid, high = [1,2,3], [4,5,6,7], [8,9] | |
chara.point += | |
case chara.level | |
when *low | |
10 | |
when *mid | |
5 | |
when *high | |
3 | |
else | |
0 | |
end | |
end | |
def bonus_point6(chara) | |
low, mid, high = [1,2,3], [4,5,6,7], [8,9] | |
chara.point += | |
case chara.level | |
when *low then 10 | |
when *mid then 5 | |
when *high then 3 | |
else 0 | |
end | |
end | |
def bonus_point7(chara) | |
low, mid, high = [1,2,3], [4,5,6,7], [8,9] | |
chara.point += | |
case chara.level | |
when *low ; 10 | |
when *mid ; 5 | |
when *high ; 3 | |
else 0 | |
end | |
end | |
def bonus_point8(chara) | |
low, mid, high = [1,2,3], [4,5,6,7], [8,9] | |
chara.point += begin | |
case chara.level | |
when *low ; 10 | |
when *mid ; 5 | |
when *high ; 3 | |
else 0 | |
end | |
end | |
end | |
def bonus_point9(chara) | |
is_low = ->lv{(1..3).include? lv} | |
is_mid = ->lv{(4..7).include? lv} | |
is_high = ->lv{(8..9).include? lv} | |
chara.point += | |
case chara.level | |
when is_low ; 10 | |
when is_mid ; 5 | |
when is_high ; 3 | |
else 0 | |
end | |
end | |
def bonus_point10(chara) | |
chara.point += | |
case chara.level | |
when low10? ; 10 | |
when mid10? ; 5 | |
when high10? ; 3 | |
else 0 | |
end | |
end | |
def low10? | |
-> lv {(1..3).include? lv} | |
end | |
def mid10? | |
-> lv {(4..7).include? lv} | |
end | |
def high10? | |
-> lv {(8..9).include? lv} | |
end | |
def bonus_point11(chara) | |
chara.point += | |
case chara.level | |
when low11? ; 10 | |
when mid11? ; 5 | |
when high11? ; 3 | |
else 0 | |
end | |
end | |
def level_seed(range) | |
-> lv {range.include? lv} | |
end | |
def low11? | |
level_seed(1..3) | |
end | |
def mid11? | |
level_seed(4..7) | |
end | |
def high11? | |
level_seed(8..9) | |
end | |
#charlie = Character.new('Charlie', 5, 0) | |
#liz = Character.new('Liz', 3, 0) | |
#ben = Character.new('Ben', 8, 0) | |
# | |
#charas = [charlie, liz, ben] | |
#charas.each {|c| bonus_point c} | |
#puts charas | |
# | |
#expected_result = <<-EOS | |
#Charlie: lv:5 pt:5 | |
#Liz: lv:3 pt:10 | |
#Ben: lv:8 pt:3 | |
#EOS | |
# | |
#charas.map(&:to_s).join("\n") == expected_result.chomp | |
def prepare_charas | |
charlie = Character.new('Charlie', 5, 0) | |
liz = Character.new('Liz', 3, 0) | |
ben = Character.new('Ben', 8, 0) | |
[charlie, liz, ben] | |
end | |
puts "RUBY_VERSION: #{RUBY_VERSION}" | |
n = 10000 | |
Benchmark.bm(8) do |x| | |
x.report("bonus_point0:") { | |
charas = prepare_charas | |
n.times { charas.each {|c| bonus_point0 c} } | |
} | |
x.report("bonus_point1:") { | |
charas = prepare_charas | |
n.times { charas.each {|c| bonus_point1 c} } | |
} | |
x.report("bonus_point2:") { | |
charas = prepare_charas | |
n.times { charas.each {|c| bonus_point2 c} } | |
} | |
x.report("bonus_point3:") { | |
charas = prepare_charas | |
n.times { charas.each {|c| bonus_point3 c} } | |
} | |
x.report("bonus_point4:") { | |
charas = prepare_charas | |
n.times { charas.each {|c| bonus_point4 c} } | |
} | |
x.report("bonus_point5:") { | |
charas = prepare_charas | |
n.times { charas.each {|c| bonus_point5 c} } | |
} | |
x.report("bonus_point6:") { | |
charas = prepare_charas | |
n.times { charas.each {|c| bonus_point6 c} } | |
} | |
x.report("bonus_point7:") { | |
charas = prepare_charas | |
n.times { charas.each {|c| bonus_point7 c} } | |
} | |
x.report("bonus_point8:") { | |
charas = prepare_charas | |
n.times { charas.each {|c| bonus_point7 c} } | |
} | |
x.report("bonus_point9:") { | |
charas = prepare_charas | |
n.times { charas.each {|c| bonus_point9 c} } | |
} | |
x.report("bonus_point10:") { | |
charas = prepare_charas | |
n.times { charas.each {|c| bonus_point10 c} } | |
} | |
x.report("bonus_point11:") { | |
charas = prepare_charas | |
n.times { charas.each {|c| bonus_point11 c} } | |
} | |
end | |
# >> RUBY_VERSION: 2.1.1 | |
# >> user system total real | |
# >> bonus_point0: 0.030000 0.000000 0.030000 ( 0.034863) | |
# >> bonus_point1: 0.030000 0.000000 0.030000 ( 0.024595) | |
# >> bonus_point2: 0.010000 0.000000 0.010000 ( 0.013378) | |
# >> bonus_point3: 0.030000 0.000000 0.030000 ( 0.025469) | |
# >> bonus_point4: 0.040000 0.000000 0.040000 ( 0.043132) | |
# >> bonus_point5: 0.040000 0.000000 0.040000 ( 0.043047) | |
# >> bonus_point6: 0.050000 0.000000 0.050000 ( 0.049851) | |
# >> bonus_point7: 0.050000 0.000000 0.050000 ( 0.043896) | |
# >> bonus_point8: 0.040000 0.000000 0.040000 ( 0.041393) | |
# >> bonus_point9: 0.080000 0.000000 0.080000 ( 0.080405) | |
# >> bonus_point10: 0.080000 0.000000 0.080000 ( 0.087997) | |
# >> bonus_point11: 0.100000 0.000000 0.100000 ( 0.093977) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment