-
-
Save melborne/9428180 to your computer and use it in GitHub Desktop.
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
# use benchmark_suite | |
# https://github.com/evanphx/benchmark_suite | |
# -*- encoding: utf-8 -*- | |
require 'benchmark/ips' | |
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 = 1 | |
Benchmark.ips 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.0 | |
# >> Calculating ------------------------------------- | |
# >> bonus_point0: 10999 i/100ms | |
# >> bonus_point1: 10451 i/100ms | |
# >> bonus_point2: 13194 i/100ms | |
# >> bonus_point3: 10767 i/100ms | |
# >> bonus_point4: 8022 i/100ms | |
# >> bonus_point5: 8009 i/100ms | |
# >> bonus_point6: 7967 i/100ms | |
# >> bonus_point7: 7964 i/100ms | |
# >> bonus_point8: 6660 i/100ms | |
# >> bonus_point9: 4587 i/100ms | |
# >> bonus_point10: 4336 i/100ms | |
# >> bonus_point11: 4362 i/100ms | |
# >> ------------------------------------------------- | |
# >> bonus_point0: 152961.7 (±1.9%) i/s - 769930 in 5.035400s | |
# >> bonus_point1: 153844.6 (±1.8%) i/s - 773374 in 5.028632s | |
# >> bonus_point2: 212334.4 (±2.2%) i/s - 1068714 in 5.035788s | |
# >> bonus_point3: 148210.8 (±2.3%) i/s - 742923 in 5.015297s | |
# >> bonus_point4: 99840.2 (±0.9%) i/s - 505386 in 5.062398s | |
# >> bonus_point5: 97060.8 (±6.0%) i/s - 488549 in 5.058897s | |
# >> bonus_point6: 98541.1 (±1.4%) i/s - 493954 in 5.013613s | |
# >> bonus_point7: 98243.9 (±2.1%) i/s - 493768 in 5.028145s | |
# >> bonus_point8: 98878.2 (±1.6%) i/s - 499500 in 5.052993s | |
# >> bonus_point9: 53336.4 (±1.7%) i/s - 270633 in 5.075565s | |
# >> bonus_point10: 49472.2 (±1.9%) i/s - 251488 in 5.085225s | |
# >> bonus_point11: 48240.6 (±2.3%) i/s - 244272 in 5.066460s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment