Created
September 12, 2010 17:47
-
-
Save eregon/576279 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment