Skip to content

Instantly share code, notes, and snippets.

@mikecmpbll
Created April 9, 2015 11:16
Show Gist options
  • Save mikecmpbll/45103c300279e929f00d to your computer and use it in GitHub Desktop.
Save mikecmpbll/45103c300279e929f00d to your computer and use it in GitHub Desktop.
irb(main):006:0> Benchmark.measure { 100_000.times { [1, 2, 2, 3, 3, 3].each_with_object(Hash.new{ |x,k| x[k] = 0 }){ |x,s| s[x] += 1 }.max_by{ |k,v| v }[0] } }
=> #<Benchmark::Tms:0x007fc3e180b108 @label="", @real=0.899188, @cstime=0.0, @cutime=0.0, @stime=0.010000000000000009, @utime=0.8799999999999999, @total=0.8899999999999999>
irb(main):007:0> Benchmark.measure { 100_000.times { [1, 2, 2, 3, 3, 3].group_by{|e| e}.values.max_by(&:size).first } }
=> #<Benchmark::Tms:0x007fc3dffbe7d0 @label="", @real=0.523452, @cstime=0.0, @cutime=0.0, @stime=0.010000000000000009, @utime=0.5099999999999998, @total=0.5199999999999998>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment