daily pastebin goal
53%
SHARE
TWEET

Untitled

a guest May 30th, 2017 100 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.  
  19. MADLEP_BYTE_CACHE = ["z".bytes.first, 0]
  20. POINTS.each do |k, v|
  21.   MADLEP_BYTE_CACHE[k.bytes.first] = v
  22.   MADLEP_BYTE_CACHE[k.bytes.first + 32] = v
  23. end
  24. MADLEP_BYTE_CACHE.freeze
  25.  
  26. class Letter
  27.   attr_reader :letter
  28.   def initialize letter
  29.     @letter = letter
  30.   end
  31.  
  32.   def score
  33.     POINTS[letter.upcase] || 0
  34.   end
  35. end
  36.  
  37. class Word
  38.   attr_reader :word
  39.  
  40.   def initialize word
  41.     @word = word
  42.   end
  43.  
  44.   def score
  45.     word.to_s.chars.map { |l| Letter.new(l).score }.inject(:+).to_i
  46.   end
  47. end
  48.  
  49. POINTS_BY_FIXNUM = POINTS.map{ |k, v| [k.bytes.first, v] }.to_h
  50.  
  51. ASCII_CAPITALISATION_BITMASK = (255 - 32) # Mask off all but bit 6
  52.  
  53. class Scrabble
  54.   def self.hackling(word)
  55.     word ||= ""
  56.     return 0 if word == ""
  57.  
  58.     word.upcase.chars.map {|char| POINTS[char] }.inject(&:+)
  59.   end
  60.  
  61.   def self.jma(word)
  62.     String(word).upcase.each_char.map { |char| POINTS[char] }.inject(0, :+)
  63.   end
  64.  
  65.   def self.fuzzmonkey(word)
  66.     word.to_s.chars.map{|c| POINTS[c.upcase].to_i }.inject(:+).to_i
  67.   end
  68.  
  69.   def self.hybrid(word)
  70.     # I thought upcasing in the block might be faster by saving a pass through the array
  71.     String(word).each_char.map { |char| POINTS[char.upcase] }.inject(0, :+)
  72.   end
  73.  
  74.   def self.hybrid2(word)
  75.     word ||= ""
  76.     return 0 if word == ""
  77.  
  78.     word.chars.map {|char| POINTS[char.upcase] }.inject(&:+)
  79.   end
  80.  
  81.   def self.parameme(word)
  82.     return 0 unless (word && word.is_a?(String) && word.size > 0)
  83.     word.each_byte.reduce(0) { |_, byte| _ + POINTS_BY_FIXNUM.fetch(byte & ASCII_CAPITALISATION_BITMASK) }
  84.   end
  85.  
  86.   def self.mp_oop(word)
  87.     Word.new(word).score
  88.   end
  89.  
  90.   def self.mp_flat(word)
  91.     sum = 0
  92.     word&.each_byte do |b|
  93.       sum += MP_BYTE_CACHE[b] || 0
  94.     end
  95.     sum
  96.   end
  97.  
  98.   def self.madlep(word)
  99.     return 0 unless word
  100.     i = -1
  101.     sum = 0
  102.     while c = word.getbyte(i += 1)
  103.       sum += MADLEP_BYTE_CACHE[c]
  104.     end
  105.     sum
  106.   end
  107. end
  108.  
  109. require "fruity"
  110.  
  111. word = "qwertyuiopasdfghjklzxcvbnm"
  112. compare do
  113.   hackling { Scrabble.hackling(word) }
  114.   jma { Scrabble.jma(word) }
  115.   fuzzmonkey { Scrabble.fuzzmonkey(word) }
  116.   hybrid { Scrabble.hybrid(word) }
  117.   hybrid2 { Scrabble.hybrid2(word) }
  118.   parameme { Scrabble.parameme(word) }
  119.   mp_oop { Scrabble.mp_oop(word) }
  120.   mp_flat { Scrabble.mp_flat(word) }
  121.   madlep { Scrabble.madlep(word) }
  122. end
  123.  
  124. TIMES = 500_000
  125. require 'benchmark'
  126. word = "qwertyuiopasdfghjklzxcvbnm"
  127.  
  128. puts "Number of Times: #{TIMES}"
  129. puts ""
  130. puts "Long Word"
  131. word = "qwertyuiopasdfghjklzxcvbnm"
  132. Benchmark.bm(12) do |x|
  133.   x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
  134.   x.report("     jma: ") { TIMES.times { Scrabble.jma(word) } }
  135.   x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
  136.   x.report("  hybrid: ") { TIMES.times { Scrabble.hybrid(word) } }
  137.   x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
  138.   x.report("parameme: ") { TIMES.times { Scrabble.parameme(word) } }
  139.   x.report("  mp_oop: ") { TIMES.times { Scrabble.mp_oop(word) } }
  140.   x.report(" mp_flat: ") { TIMES.times { Scrabble.mp_flat(word) } }
  141.   x.report("  madlep: ") { TIMES.times { Scrabble.madlep(word) } }
  142. end
  143.  
  144. puts ""
  145. puts "Short Word"
  146. word = "qwerty"
  147. Benchmark.bm(12) do |x|
  148.   x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
  149.   x.report("     jma: ") { TIMES.times { Scrabble.jma(word) } }
  150.   x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
  151.   x.report("  hybrid: ") { TIMES.times { Scrabble.hybrid(word) } }
  152.   x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
  153.   x.report("parameme: ") { TIMES.times { Scrabble.parameme(word) } }
  154.   x.report("  mp_oop: ") { TIMES.times { Scrabble.mp_oop(word) } }
  155.   x.report(" mp_flat: ") { TIMES.times { Scrabble.mp_flat(word) } }
  156.   x.report("  madlep: ") { TIMES.times { Scrabble.madlep(word) } }
  157. end
  158.  
  159. puts ""
  160. puts "Nil Word"
  161. word = nil
  162. Benchmark.bm(12) do |x|
  163.   x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
  164.   x.report("     jma: ") { TIMES.times { Scrabble.jma(word) } }
  165.   x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
  166.   x.report("  hybrid: ") { TIMES.times { Scrabble.hybrid(word) } }
  167.   x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
  168.   x.report("parameme: ") { TIMES.times { Scrabble.parameme(word) } }
  169.   x.report("  mp_oop: ") { TIMES.times { Scrabble.mp_oop(word) } }
  170.   x.report(" mp_flat: ") { TIMES.times { Scrabble.mp_flat(word) } }
  171.   x.report("  madlep: ") { TIMES.times { Scrabble.madlep(word) } }
  172. end
  173.  
  174. puts ""
  175. puts "Empty Word"
  176. word = ""
  177. Benchmark.bm(12) do |x|
  178.   x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
  179.   x.report("     jma: ") { TIMES.times { Scrabble.jma(word) } }
  180.   x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
  181.   x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
  182.   x.report("parameme: ") { TIMES.times { Scrabble.parameme(word) } }
  183.   x.report("  mp_oop: ") { TIMES.times { Scrabble.mp_oop(word) } }
  184.   x.report(" mp_flat: ") { TIMES.times { Scrabble.mp_flat(word) } }
  185.   x.report("  madlep: ") { TIMES.times { Scrabble.madlep(word) } }
  186. 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
 
Top