Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save markmcspadden/2656171 to your computer and use it in GitHub Desktop.
Save markmcspadden/2656171 to your computer and use it in GitHub Desktop.
Benchmarking different ways to transform keys. Run on 100,000 key hash.
class Hash
# Proposed solution
def transform_keys
result = {}
keys.each do |key|
result[yield(key)] = self[key]
end
result
end
def transform_keys!
keys.each do |key|
self[yield(key)] = delete(key)
end
self
end
# Alternate solution
def transform_keys_with_send(method_name)
result = {}
keys.each do |key|
result[key.send(method_name)] = self[key]
end
result
end
def transform_keys_with_send!(method_name)
keys.each do |key|
self[key.send(method_name)] = delete(key)
end
self
end
# Original solution
def stringify_keys
result = {}
keys.each do |key|
result[key.to_s] = self[key]
end
result
end
def stringify_keys!
keys.each do |key|
self[key.to_s] = delete(key)
end
self
end
end
require 'benchmark'
hash = Hash[(1..100000).collect{ |i| [i,i] } ]
puts "stringify_keys"
Benchmark.bmbm(15) do |x|
x.report("block:") { hash.transform_keys{ |k| k.to_s} }
x.report("method send:") { hash.transform_keys_with_send(:to_s) }
x.report("old way:") { hash.stringify_keys }
end
puts "\n\n"
puts "stringify_keys!"
Benchmark.bmbm(15) do |x|
x.report("block:") { hash.transform_keys!{ |k| k.to_s } }
x.report("method send:") { hash.transform_keys_with_send!(:to_s) }
x.report("old way:") { hash.stringify_keys! }
end
puts "____________"
puts "OTHER BENCHMARKS"
puts "____________"
n = 1_000_000
[ {:k => :v, :a => :b, :c => :d, :e => :f, 'j' => 'k', 'm' => 'n'} ].each do |options|
Benchmark.bmbm do |bm|
bm.report "stringify_keys" do
n.times do
options.stringify_keys
end
end
bm.report "transform_keys" do
n.times do
options.transform_keys(&:to_s)
end
end
bm.report "transform_keys with proper block" do
n.times do
options.transform_keys{ |k| k.to_s }
end
end
bm.report "transform_keys_with_send" do
n.times do
options.transform_keys_with_send(:to_s)
end
end
bm.report "stringify_keys!" do
n.times do
options.stringify_keys!
end
end
bm.report "transform_keys!" do
n.times do
options.transform_keys!(&:to_s)
end
end
bm.report "transform_keys with proper block!" do
n.times do
options.transform_keys!{ |k| k.to_s }
end
end
bm.report "transform_keys_with_send!" do
n.times do
options.transform_keys_with_send!(:to_s)
end
end
end
end
stringify_keys
Rehearsal ---------------------------------------------------
block: 0.150000 0.020000 0.170000 ( 0.157368)
method send: 0.160000 0.010000 0.170000 ( 0.170694)
old way: 0.160000 0.000000 0.160000 ( 0.160916)
------------------------------------------ total: 0.500000sec
user system total real
block: 0.140000 0.000000 0.140000 ( 0.141677)
method send: 0.130000 0.000000 0.130000 ( 0.135184)
old way: 0.140000 0.000000 0.140000 ( 0.134680)
stringify_keys!
Rehearsal ---------------------------------------------------
block: 0.170000 0.010000 0.180000 ( 0.173217)
method send: 0.150000 0.000000 0.150000 ( 0.156781)
old way: 0.140000 0.000000 0.140000 ( 0.132320)
------------------------------------------ total: 0.470000sec
user system total real
block: 0.140000 0.000000 0.140000 ( 0.136851)
method send: 0.120000 0.000000 0.120000 ( 0.123499)
old way: 0.120000 0.010000 0.130000 ( 0.125058)
____________
OTHER BENCHMARKS
____________
Rehearsal ---------------------------------------------------------------------
stringify_keys 5.830000 0.080000 5.910000 ( 5.919836)
transform_keys 6.330000 0.080000 6.410000 ( 6.423611)
transform_keys with proper block 6.170000 0.080000 6.250000 ( 6.246085)
transform_keys_with_send 5.940000 0.080000 6.020000 ( 6.030339)
stringify_keys! 3.930000 0.040000 3.970000 ( 3.967233)
transform_keys! 4.270000 0.030000 4.300000 ( 4.311692)
transform_keys with proper block! 4.150000 0.030000 4.180000 ( 4.185313)
transform_keys_with_send! 3.870000 0.040000 3.910000 ( 3.903076)
----------------------------------------------------------- total: 40.950000sec
user system total real
stringify_keys 4.770000 0.100000 4.870000 ( 4.875548)
transform_keys 5.230000 0.100000 5.330000 ( 5.340638)
transform_keys with proper block 4.970000 0.090000 5.060000 ( 5.076670)
transform_keys_with_send 4.990000 0.100000 5.090000 ( 5.083732)
stringify_keys! 3.720000 0.020000 3.740000 ( 3.737684)
transform_keys! 4.070000 0.020000 4.090000 ( 4.102100)
transform_keys with proper block! 3.990000 0.030000 4.020000 ( 4.019291)
transform_keys_with_send! 3.820000 0.020000 3.840000 ( 3.847108)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment