SHARE
TWEET

Untitled

a guest May 30th, 2017 55 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. POINTS_BY_FIXNUM = POINTS.map{ |k, v| [k.bytes.first, v] }.to_h
  12.  
  13. ASCII_CAPITALISATION_BITMASK = (255 - 32) # Mask off all but bit 6
  14.  
  15. class Scrabble
  16.   def self.hackling(word)
  17.     word ||= ""
  18.     return 0 if word == ""
  19.  
  20.     word.upcase.chars.map {|char| POINTS[char] }.inject(&:+)
  21.   end
  22.  
  23.   def self.jma(word)
  24.     String(word).upcase.each_char.map { |char| POINTS[char] }.inject(0, :+)
  25.   end
  26.  
  27.   def self.fuzzmonkey(word)
  28.     word.to_s.chars.map{|c| POINTS[c.upcase].to_i }.inject(:+).to_i
  29.   end
  30.  
  31.   def self.hybrid(word)
  32.     # I thought upcasing in the block might be faster by saving a pass through the array
  33.     String(word).each_char.map { |char| POINTS[char.upcase] }.inject(0, :+)
  34.   end
  35.  
  36.   def self.hybrid2(word)
  37.     word ||= ""
  38.     return 0 if word == ""
  39.  
  40.     word.chars.map {|char| POINTS[char.upcase] }.inject(&:+)
  41.   end
  42.  
  43.   def self.parameme(word)
  44.     return 0 unless (word && word.is_a?(String) && word.size > 0)
  45.     word.each_byte.reduce(0) { |_, byte| _ + POINTS_BY_FIXNUM.fetch(byte & ASCII_CAPITALISATION_BITMASK) }
  46.   end
  47. end
  48.  
  49. require "fruity"
  50.  
  51. word = "qwertyuiopasdfghjklzxcvbnm"
  52. compare do
  53.   hackling { Scrabble.hackling(word) }
  54.   jma { Scrabble.jma(word) }
  55.   fuzzmonkey { Scrabble.fuzzmonkey(word) }
  56.   hybrid { Scrabble.hybrid(word) }
  57.   hybrid2 { Scrabble.hybrid2(word) }
  58.   parameme { Scrabble.parameme(word) }
  59. end
  60.  
  61. TIMES = 500_000
  62. require 'benchmark'
  63. word = "qwertyuiopasdfghjklzxcvbnm"
  64.  
  65. puts "Number of Times: #{TIMES}"
  66. puts ""
  67. puts "Long Word"
  68. word = "qwertyuiopasdfghjklzxcvbnm"
  69. Benchmark.bm(12) do |x|
  70.   x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
  71.   x.report("     jma: ") { TIMES.times { Scrabble.jma(word) } }
  72.   x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
  73.   x.report("  hybrid: ") { TIMES.times { Scrabble.hybrid(word) } }
  74.   x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
  75.   x.report("parameme: ") { TIMES.times { Scrabble.parameme(word) } }
  76. end
  77.  
  78. puts ""
  79. puts "Short Word"
  80. word = "qwerty"
  81. Benchmark.bm(12) do |x|
  82.   x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
  83.   x.report("     jma: ") { TIMES.times { Scrabble.jma(word) } }
  84.   x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
  85.   x.report("  hybrid: ") { TIMES.times { Scrabble.hybrid(word) } }
  86.   x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
  87.   x.report("parameme: ") { TIMES.times { Scrabble.parameme(word) } }
  88. end
  89.  
  90. puts ""
  91. puts "Nil Word"
  92. word = nil
  93. Benchmark.bm(12) do |x|
  94.   x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
  95.   x.report("     jma: ") { TIMES.times { Scrabble.jma(word) } }
  96.   x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
  97.   x.report("  hybrid: ") { TIMES.times { Scrabble.hybrid(word) } }
  98.   x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
  99.   x.report("parameme: ") { TIMES.times { Scrabble.parameme(word) } }
  100. end
  101.  
  102. puts ""
  103. puts "Empty Word"
  104. word = ""
  105. Benchmark.bm(12) do |x|
  106.   x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
  107.   x.report("     jma: ") { TIMES.times { Scrabble.jma(word) } }
  108.   x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
  109.   x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
  110.   x.report("parameme: ") { TIMES.times { Scrabble.parameme(word) } }
  111. 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