Created
June 28, 2012 00:06
-
-
Save nertzy/3007749 to your computer and use it in GitHub Desktop.
Faster stringify_keys and symbolize_keys
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_relative "lib/active_support/core_ext/hash/keys" | |
require "benchmark" | |
[1, 10, 100, 1_000, 10_000, 100_000, 1_000_000].each do |count| | |
puts | |
puts '*' * 80 | |
puts "Hash with #{count} entries" | |
puts | |
strings = (1..count).map(&:to_s) | |
symbols = strings.map(&:to_sym) | |
hash1 = Hash[symbols.zip(strings)] | |
hash2 = hash1.dup | |
hash3 = Hash[strings.zip(strings)] | |
hash4 = hash3.dup | |
def hash2.transform_keys | |
result = {} | |
each_key do |key| | |
result[yield(key)] = self[key] | |
end | |
result | |
end | |
def hash4.transform_keys | |
result = {} | |
each_key do |key| | |
result[yield(key)] = self[key] | |
end | |
result | |
end | |
raise unless hash1.stringify_keys == hash2.stringify_keys | |
raise unless hash3.symbolize_keys == hash4.symbolize_keys | |
Benchmark.bm(17) do |x| | |
x.report "original stringify" do | |
hash1.stringify_keys | |
end | |
x.report "each_key stringify" do | |
hash2.stringify_keys | |
end | |
x.report "original symbolize" do | |
hash3.symbolize_keys | |
end | |
x.report "each_key symbolize" do | |
hash4.symbolize_keys | |
end | |
end | |
puts | |
GC.disable | |
count1 = ObjectSpace.each_object {} | |
hash1.stringify_keys | |
count2 = ObjectSpace.each_object {} | |
hash2.stringify_keys | |
count3 = ObjectSpace.each_object {} | |
hash3.symbolize_keys | |
count4 = ObjectSpace.each_object {} | |
hash4.symbolize_keys | |
count5 = ObjectSpace.each_object {} | |
puts "original stringify #{count2 - count1} objects created" | |
puts "each_key stringify #{count3 - count2} objects created" | |
puts "original symbolize #{count5 - count3} objects created" | |
puts "each_key symbolize #{count5 - count4} objects created" | |
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
grantmac:activesupport grant$ ruby --version | |
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.3.0] | |
grantmac:activesupport grant$ ruby hash_test.rb | |
******************************************************************************** | |
Hash with 1 entries | |
user system total real | |
original stringify 0.000000 0.000000 0.000000 ( 0.000010) | |
each_key stringify 0.000000 0.000000 0.000000 ( 0.000007) | |
original symbolize 0.000000 0.000000 0.000000 ( 0.000007) | |
each_key symbolize 0.000000 0.000000 0.000000 ( 0.000006) | |
original stringify 4 objects created | |
each_key stringify 3 objects created | |
original symbolize 3 objects created | |
each_key symbolize 1 objects created | |
******************************************************************************** | |
Hash with 10 entries | |
user system total real | |
original stringify 0.000000 0.000000 0.000000 ( 0.000016) | |
each_key stringify 0.000000 0.000000 0.000000 ( 0.000014) | |
original symbolize 0.000000 0.000000 0.000000 ( 0.000020) | |
each_key symbolize 0.000000 0.000000 0.000000 ( 0.000014) | |
original stringify 22 objects created | |
each_key stringify 21 objects created | |
original symbolize 3 objects created | |
each_key symbolize 1 objects created | |
******************************************************************************** | |
Hash with 100 entries | |
user system total real | |
original stringify 0.000000 0.000000 0.000000 ( 0.000104) | |
each_key stringify 0.000000 0.000000 0.000000 ( 0.000114) | |
original symbolize 0.000000 0.000000 0.000000 ( 0.000104) | |
each_key symbolize 0.000000 0.000000 0.000000 ( 0.000096) | |
original stringify 202 objects created | |
each_key stringify 201 objects created | |
original symbolize 3 objects created | |
each_key symbolize 1 objects created | |
******************************************************************************** | |
Hash with 1000 entries | |
user system total real | |
original stringify 0.000000 0.000000 0.000000 ( 0.001030) | |
each_key stringify 0.000000 0.000000 0.000000 ( 0.000955) | |
original symbolize 0.000000 0.000000 0.000000 ( 0.000985) | |
each_key symbolize 0.000000 0.000000 0.000000 ( 0.000925) | |
original stringify 2002 objects created | |
each_key stringify 2001 objects created | |
original symbolize 3 objects created | |
each_key symbolize 1 objects created | |
******************************************************************************** | |
Hash with 10000 entries | |
user system total real | |
original stringify 0.010000 0.000000 0.010000 ( 0.011323) | |
each_key stringify 0.010000 0.000000 0.010000 ( 0.011096) | |
original symbolize 0.010000 0.000000 0.010000 ( 0.011501) | |
each_key symbolize 0.010000 0.000000 0.010000 ( 0.011780) | |
original stringify 20002 objects created | |
each_key stringify 20001 objects created | |
original symbolize 3 objects created | |
each_key symbolize 1 objects created | |
******************************************************************************** | |
Hash with 100000 entries | |
user system total real | |
original stringify 0.150000 0.010000 0.160000 ( 0.153056) | |
each_key stringify 0.130000 0.010000 0.140000 ( 0.139755) | |
original symbolize 0.160000 0.000000 0.160000 ( 0.161441) | |
each_key symbolize 0.140000 0.000000 0.140000 ( 0.147776) | |
original stringify 200002 objects created | |
each_key stringify 200001 objects created | |
original symbolize 3 objects created | |
each_key symbolize 1 objects created | |
******************************************************************************** | |
Hash with 1000000 entries | |
user system total real | |
original stringify 2.460000 0.090000 2.550000 ( 2.554547) | |
each_key stringify 2.190000 0.080000 2.270000 ( 2.279136) | |
original symbolize 2.400000 0.040000 2.440000 ( 2.429980) | |
each_key symbolize 2.080000 0.030000 2.110000 ( 2.118653) | |
original stringify 2000002 objects created | |
each_key stringify 2000001 objects created | |
original symbolize 3 objects created | |
each_key symbolize 1 objects created |
To speed up Rails for everyone, everywhere.
Interesting, I run this locally on my machine and consistently get results that show the original is faster.
How about object creation? I think that's more important in the long term than the speed. Less GC over time.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Motive?