public
Last active

  • Download Gist
hash_max.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
require 'benchmark'
 
N = 10000
HASH_SIZE = 10
 
HASH = {}
HASH_SIZE.times do |n|
HASH[n] = (rand * 100).round - 50
end
puts HASH.inspect
 
if RUBY_VERSION < '1.9'
class Array
def keys
map(&:first)
end
end
end
 
Benchmark.bm(30) do |bm|
bm.instance_exec do
def test label
report(label) {
N.times {
yield
}
}
end
def test19 label, &block
if RUBY_VERSION > '1.9'
test "#{label} (1.9)", &block
end
end
 
test('max') { HASH.max { |a,b| a.last <=> b.last}.first }
test('max (expanded)') { HASH.max { |(k1,v1),(k2,v2)| v1 <=> v2 }.first }
test('max_by') { HASH.max_by(&:last).first }
test('max_by (expanded)') { HASH.max_by { |k,v| v }.first }
 
test('select') { HASH.select { |x,i| i == HASH.values.max }.keys }
test('sort_by') { HASH.sort_by { |k,v| v }.last.first }
test('invert') { HASH.invert[HASH.values.max] }
if RUBY_VERSION > '1.9' # Hash#key was Hash#index in 1.8
test('key') { HASH.key HASH.values.max }
else
test('key') { HASH.index HASH.values.max }
end
 
puts
report('select (cache max)') do
max = HASH.values.max
N.times { HASH.select { |x,i| i == max }.keys }
end
report('invert (cache max)') do
max = HASH.values.max
N.times { HASH.invert[max] }
end
if RUBY_VERSION > '1.9'
report('key (cache max)') do
max = HASH.values.max
N.times { HASH.key max }
end
else
report('key (cache max)') do
max = HASH.values.max
N.times { HASH.index max }
end
end
end
end
results 1.8
1 2 3 4 5 6 7 8 9 10 11 12 13 14
{5=>-13, 0=>-31, 6=>47, 1=>-47, 7=>-36, 2=>5, 8=>-32, 3=>-29, 9=>38, 4=>-21}
user system total real
max 0.230000 0.000000 0.230000 ( 0.228710)
max (expanded) 0.240000 0.000000 0.240000 ( 0.244368)
max_by 0.250000 0.000000 0.250000 ( 0.254624)
max_by (expanded) 0.160000 0.000000 0.160000 ( 0.168497)
select 0.810000 0.010000 0.820000 ( 0.850495)
sort_by 0.210000 0.000000 0.210000 ( 0.215644)
invert 0.110000 0.000000 0.110000 ( 0.108169)
key 0.060000 0.000000 0.060000 ( 0.070295)
 
select (cache max) 0.210000 0.000000 0.210000 ( 0.210509)
invert (cache max) 0.050000 0.000000 0.050000 ( 0.048558)
key (cache max) 0.010000 0.000000 0.010000 ( 0.008889)
results 1.9
1 2 3 4 5 6 7 8 9 10 11 12 13 14
{0=>48, 1=>-48, 2=>1, 3=>-8, 4=>37, 5=>-23, 6=>-11, 7=>-37, 8=>3, 9=>13}
user system total real
max 0.060000 0.000000 0.060000 ( 0.051559)
max (expanded) 0.040000 0.000000 0.040000 ( 0.041429)
max_by 0.040000 0.000000 0.040000 ( 0.038113)
max_by (expanded) 0.040000 0.000000 0.040000 ( 0.040344)
select 0.260000 0.000000 0.260000 ( 0.264688)
sort_by 0.070000 0.000000 0.070000 ( 0.070121)
invert 0.070000 0.000000 0.070000 ( 0.069767)
key 0.030000 0.000000 0.030000 ( 0.030712)
 
select (cache max) 0.030000 0.000000 0.030000 ( 0.031417)
invert (cache max) 0.040000 0.010000 0.050000 ( 0.038634)
key (cache max) 0.000000 0.000000 0.000000 ( 0.002110)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.