SHARE
TWEET

Untitled

a guest May 30th, 2017 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. POINTS = {
  2.   "A" => 1, "B" => 3, "C" => 3, "D" => 2,
  3.   "E" => 1, "F" => 4, "G" => 2, "H" => 4,
  4.   "I" => 1, "J" => 8, "K" => 5, "L" => 1,
  5.   "M" => 3, "N" => 1, "O" => 1, "P" => 3,
  6.   "Q" => 10, "R" => 1, "S" => 1, "T" => 1,
  7.   "U" => 1, "V" => 4, "W" => 4, "X" => 8,
  8.   "Y" => 4, "Z" => 10
  9. }
  10.  
  11. MP_BYTE_CACHE = []
  12. POINTS.each do |k, v|
  13.   MP_BYTE_CACHE[k.bytes.first] = v
  14.   MP_BYTE_CACHE[k.bytes.first + 32] = v
  15. end
  16. MP_BYTE_CACHE.freeze
  17.  
  18. class Letter
  19.   attr_reader :letter
  20.   def initialize letter
  21.     @letter = letter
  22.   end
  23.  
  24.   def score
  25.     POINTS[letter.upcase] || 0
  26.   end
  27. end
  28.  
  29. class Word
  30.   attr_reader :word
  31.  
  32.   def initialize word
  33.     @word = word
  34.   end
  35.  
  36.   def score
  37.     word.to_s.chars.map { |l| Letter.new(l).score }.inject(:+).to_i
  38.   end
  39. end
  40.  
  41. POINTS_BY_FIXNUM = POINTS.map{ |k, v| [k.bytes.first, v] }.to_h
  42.  
  43. ASCII_CAPITALISATION_BITMASK = (255 - 32) # Mask off all but bit 6
  44.  
  45. class Scrabble
  46.   def self.hackling(word)
  47.     word ||= ""
  48.     return 0 if word == ""
  49.  
  50.     word.upcase.chars.map {|char| POINTS[char] }.inject(&:+)
  51.   end
  52.  
  53.   def self.jma(word)
  54.     String(word).upcase.each_char.map { |char| POINTS[char] }.inject(0, :+)
  55.   end
  56.  
  57.   def self.fuzzmonkey(word)
  58.     word.to_s.chars.map{|c| POINTS[c.upcase].to_i }.inject(:+).to_i
  59.   end
  60.  
  61.   def self.hybrid(word)
  62.     # I thought upcasing in the block might be faster by saving a pass through the array
  63.     String(word).each_char.map { |char| POINTS[char.upcase] }.inject(0, :+)
  64.   end
  65.  
  66.   def self.hybrid2(word)
  67.     word ||= ""
  68.     return 0 if word == ""
  69.  
  70.     word.chars.map {|char| POINTS[char.upcase] }.inject(&:+)
  71.   end
  72.  
  73.   def self.parameme(word)
  74.     return 0 unless (word && word.is_a?(String) && word.size > 0)
  75.     word.each_byte.reduce(0) { |_, byte| _ + POINTS_BY_FIXNUM.fetch(byte & ASCII_CAPITALISATION_BITMASK) }
  76.   end
  77.  
  78.   def self.mp_oop(word)
  79.     Word.new(word).score
  80.   end
  81.  
  82.   def self.mp_flat(word)
  83.     sum = 0
  84.     word&.each_byte do |b|
  85.       sum += MP_BYTE_CACHE[b] || 0
  86.     end
  87.     sum
  88.   end
  89. end
  90.  
  91. require "fruity"
  92.  
  93. word = "qwertyuiopasdfghjklzxcvbnm"
  94. compare do
  95.   hackling { Scrabble.hackling(word) }
  96.   jma { Scrabble.jma(word) }
  97.   fuzzmonkey { Scrabble.fuzzmonkey(word) }
  98.   hybrid { Scrabble.hybrid(word) }
  99.   hybrid2 { Scrabble.hybrid2(word) }
  100.   parameme { Scrabble.parameme(word) }
  101.   mp_oop { Scrabble.mp_oop(word) }
  102.   mp_flat { Scrabble.mp_flat(word) }
  103. end
  104.  
  105. TIMES = 500_000
  106. require 'benchmark'
  107. word = "qwertyuiopasdfghjklzxcvbnm"
  108.  
  109. puts "Number of Times: #{TIMES}"
  110. puts ""
  111. puts "Long Word"
  112. word = "qwertyuiopasdfghjklzxcvbnm"
  113. Benchmark.bm(12) do |x|
  114.   x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
  115.   x.report("     jma: ") { TIMES.times { Scrabble.jma(word) } }
  116.   x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
  117.   x.report("  hybrid: ") { TIMES.times { Scrabble.hybrid(word) } }
  118.   x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
  119.   x.report("parameme: ") { TIMES.times { Scrabble.parameme(word) } }
  120.   x.report("  mp_oop: ") { TIMES.times { Scrabble.mp_oop(word) } }
  121.   x.report(" mp_flat: ") { TIMES.times { Scrabble.mp_flat(word) } }
  122. end
  123.  
  124. puts ""
  125. puts "Short Word"
  126. word = "qwerty"
  127. Benchmark.bm(12) do |x|
  128.   x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
  129.   x.report("     jma: ") { TIMES.times { Scrabble.jma(word) } }
  130.   x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
  131.   x.report("  hybrid: ") { TIMES.times { Scrabble.hybrid(word) } }
  132.   x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
  133.   x.report("parameme: ") { TIMES.times { Scrabble.parameme(word) } }
  134.   x.report("  mp_oop: ") { TIMES.times { Scrabble.mp_oop(word) } }
  135.   x.report(" mp_flat: ") { TIMES.times { Scrabble.mp_flat(word) } }
  136. end
  137.  
  138. puts ""
  139. puts "Nil Word"
  140. word = nil
  141. Benchmark.bm(12) do |x|
  142.   x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
  143.   x.report("     jma: ") { TIMES.times { Scrabble.jma(word) } }
  144.   x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
  145.   x.report("  hybrid: ") { TIMES.times { Scrabble.hybrid(word) } }
  146.   x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
  147.   x.report("parameme: ") { TIMES.times { Scrabble.parameme(word) } }
  148.   x.report("  mp_oop: ") { TIMES.times { Scrabble.mp_oop(word) } }
  149.   x.report(" mp_flat: ") { TIMES.times { Scrabble.mp_flat(word) } }
  150. end
  151.  
  152. puts ""
  153. puts "Empty Word"
  154. word = ""
  155. Benchmark.bm(12) do |x|
  156.   x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
  157.   x.report("     jma: ") { TIMES.times { Scrabble.jma(word) } }
  158.   x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
  159.   x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
  160.   x.report("parameme: ") { TIMES.times { Scrabble.parameme(word) } }
  161.   x.report("  mp_oop: ") { TIMES.times { Scrabble.mp_oop(word) } }
  162.   x.report(" mp_flat: ") { TIMES.times { Scrabble.mp_flat(word) } }
  163. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top