-
-
Save parameme/7933b8b94588cd46dced5a0eb7eb9c52 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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 | |
} | |
POINTS_BY_FIXNUM = POINTS.map{ |k, v| [k.bytes.first, v] }.to_h | |
ASCII_CAPITALISATION_BITMASK = (255 - 32) # Mask off all but bit 6 | |
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 | |
def self.parameme(word) | |
return 0 unless (word && word.is_a?(String) && word.size > 0) | |
word.each_byte.reduce(0) { |_, byte| _ + POINTS_BY_FIXNUM.fetch(byte & ASCII_CAPITALISATION_BITMASK) } | |
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) } | |
parameme { Scrabble.parameme(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) } } | |
x.report("parameme: ") { TIMES.times { Scrabble.parameme(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) } } | |
x.report("parameme: ") { TIMES.times { Scrabble.parameme(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) } } | |
x.report("parameme: ") { TIMES.times { Scrabble.parameme(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) } } | |
x.report("parameme: ") { TIMES.times { Scrabble.parameme(word) } } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment