daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest May 30th, 2017 49 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. class Scrabble
  12.   def self.hackling(word)
  13.     word ||= ""
  14.     return 0 if word == ""
  15.  
  16.     word.upcase.chars.map {|char| POINTS[char] }.inject(&:+)
  17.   end
  18.  
  19.   def self.jma(word)
  20.     String(word).upcase.each_char.map { |char| POINTS[char] }.inject(0, :+)
  21.   end
  22.  
  23.   def self.fuzzmonkey(word)
  24.     word.to_s.chars.map{|c| POINTS[c.upcase].to_i }.inject(:+).to_i
  25.   end
  26.  
  27.   def self.hybrid(word)
  28.     # I thought upcasing in the block might be faster by saving a pass through the array
  29.     String(word).each_char.map { |char| POINTS[char.upcase] }.inject(0, :+)
  30.   end
  31.  
  32.   def self.hybrid2(word)
  33.     word ||= ""
  34.     return 0 if word == ""
  35.  
  36.     word.chars.map {|char| POINTS[char.upcase] }.inject(&:+)
  37.   end
  38. end
  39.  
  40. require "fruity"
  41.  
  42. word = "qwertyuiopasdfghjklzxcvbnm"
  43. compare do
  44.   hackling { Scrabble.hackling(word) }
  45.   jma { Scrabble.jma(word) }
  46.   fuzzmonkey { Scrabble.fuzzmonkey(word) }
  47.   hybrid { Scrabble.hybrid(word) }
  48.   hybrid2 { Scrabble.hybrid2(word) }
  49. end
  50.  
  51. TIMES = 500_000
  52. require 'benchmark'
  53. word = "qwertyuiopasdfghjklzxcvbnm"
  54.  
  55. puts "Number of Times: #{TIMES}"
  56. puts ""
  57. puts "Long Word"
  58. word = "qwertyuiopasdfghjklzxcvbnm"
  59. Benchmark.bm(12) do |x|
  60.   x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
  61.   x.report("     jma: ") { TIMES.times { Scrabble.jma(word) } }
  62.   x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
  63.   x.report("  hybrid: ") { TIMES.times { Scrabble.hybrid(word) } }
  64.   x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
  65. end
  66.  
  67. puts ""
  68. puts "Short Word"
  69. word = "qwerty"
  70. Benchmark.bm(12) do |x|
  71.   x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
  72.   x.report("     jma: ") { TIMES.times { Scrabble.jma(word) } }
  73.   x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
  74.   x.report("  hybrid: ") { TIMES.times { Scrabble.hybrid(word) } }
  75.   x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
  76. end
  77.  
  78. puts ""
  79. puts "Nil Word"
  80. word = nil
  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. end
  88.  
  89. puts ""
  90. puts "Empty Word"
  91. word = ""
  92. Benchmark.bm(12) do |x|
  93.   x.report("hackling: ") { TIMES.times { Scrabble.hackling(word) } }
  94.   x.report("     jma: ") { TIMES.times { Scrabble.jma(word) } }
  95.   x.report("fuzzmnky: ") { TIMES.times { Scrabble.fuzzmonkey(word) } }
  96.   x.report(" hybrid2: ") { TIMES.times { Scrabble.hybrid2(word) } }
  97. 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