Skip to content

Instantly share code, notes, and snippets.

@hackling
Created May 31, 2017 01:01
Show Gist options
  • Save hackling/153d3cc7c15270d9b86d19543fb6a9ea to your computer and use it in GitHub Desktop.
Save hackling/153d3cc7c15270d9b86d19543fb6a9ea to your computer and use it in GitHub Desktop.
POINTS = {
"A" => 1, "B" => 3, "C" => 3, "D" => 2,
"E" => 1, "F" => 4, "G" => 2, "H" => 4,
"I" => 1, "J" => 8, "K" => 5, "L" => 1,
"M" => 3, "N" => 1, "O" => 1, "P" => 3,
"Q" => 10, "R" => 1, "S" => 1, "T" => 1,
"U" => 1, "V" => 4, "W" => 4, "X" => 8,
"Y" => 4, "Z" => 10
}
class Scrabble
def self.hackling(word)
word ||= ""
return 0 if word == ""
word.upcase.chars.map {|char| POINTS[char] }.inject(&:+)
end
def self.jma(word)
String(word).upcase.each_char.map { |char| POINTS[char] }.inject(0, :+)
end
def self.fuzzmonkey(word)
word.to_s.chars.map{|c| POINTS[c.upcase].to_i }.inject(:+).to_i
end
def self.hybrid(word)
# I thought upcasing in the block might be faster by saving a pass through the array
String(word).each_char.map { |char| POINTS[char.upcase] }.inject(0, :+)
end
def self.hybrid2(word)
word ||= ""
return 0 if word == ""
word.chars.map {|char| POINTS[char.upcase] }.inject(&:+)
end
end
require "fruity"
word = "qwertyuiopasdfghjklzxcvbnm"
compare do
hackling { Scrabble.hackling(word) }
jma { Scrabble.jma(word) }
fuzzmonkey { Scrabble.fuzzmonkey(word) }
hybrid { Scrabble.hybrid(word) }
hybrid2 { Scrabble.hybrid2(word) }
end
TIMES = 500_000
require 'benchmark'
word = "qwertyuiopasdfghjklzxcvbnm"
puts "Number of Times: #{TIMES}"
puts ""
puts "Long Word"
word = "qwertyuiopasdfghjklzxcvbnm"
Benchmark.bm(12) do |x|
x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
x.report(" jma: ") { TIMES.times { Scrabble.jma(word) } }
x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
x.report(" hybrid: ") { TIMES.times { Scrabble.hybrid(word) } }
x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
end
puts ""
puts "Short Word"
word = "qwerty"
Benchmark.bm(12) do |x|
x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
x.report(" jma: ") { TIMES.times { Scrabble.jma(word) } }
x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
x.report(" hybrid: ") { TIMES.times { Scrabble.hybrid(word) } }
x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
end
puts ""
puts "Nil Word"
word = nil
Benchmark.bm(12) do |x|
x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
x.report(" jma: ") { TIMES.times { Scrabble.jma(word) } }
x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
x.report(" hybrid: ") { TIMES.times { Scrabble.hybrid(word) } }
x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
end
puts ""
puts "Empty Word"
word = ""
Benchmark.bm(12) do |x|
x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
x.report(" jma: ") { TIMES.times { Scrabble.jma(word) } }
x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment