Skip to content

Instantly share code, notes, and snippets.

@workmad3
Created April 9, 2015 11:25
Show Gist options
  • Save workmad3/e81cc4e371950bd8951e to your computer and use it in GitHub Desktop.
Save workmad3/e81cc4e371950bd8951e to your computer and use it in GitHub Desktop.
require 'benchmark'
ary = Array.new(1000) {rand(100)}
p Benchmark.measure { 1000.times { ary.each_with_object(Hash.new{ |x,k| x[k] = 0 }){ |x,s| s[x] += 1 }.max_by{ |k,v| v }[0] } }
p Benchmark.measure { 1000.times { ary.group_by{|e| e}.values.max_by(&:size).first } }
p Benchmark.measure { 1000.times { h = Hash.new(0); max_val = nil; max_count = 0; ary.each{ |x| count = h[x] + 1; h[x] = count; if count > max_count; max_count = count; max_val = x; end }; } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment