Skip to content

Instantly share code, notes, and snippets.

@gdiggs
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdiggs/19d8726b8bfde203327b to your computer and use it in GitHub Desktop.
Save gdiggs/19d8726b8bfde203327b to your computer and use it in GitHub Desktop.
Adding deep_dup testing
Calculating -------------------------------------
dup 5.339k i/100ms
deep_dup 4.367k i/100ms
Hash[] 5.156k i/100ms
-------------------------------------------------
dup 54.493k (± 7.2%) i/s - 272.289k
deep_dup 45.231k (± 9.1%) i/s - 227.084k
Hash[] 59.868k (± 7.5%) i/s - 299.048k
Comparison:
Hash[]: 59867.9 i/s
dup: 54493.2 i/s - 1.10x slower
deep_dup: 45230.6 i/s - 1.32x slower
Calculating -------------------------------------
bigger dup 1.506k i/100ms
bigger deep_dup 237.000 i/100ms
bigger Hash[] 2.711k i/100ms
-------------------------------------------------
bigger dup 15.771k (±11.7%) i/s - 78.312k
bigger deep_dup 2.413k (± 2.7%) i/s - 12.087k
bigger Hash[] 27.750k (±11.8%) i/s - 138.261k
Comparison:
bigger Hash[]: 27750.2 i/s
bigger dup: 15771.2 i/s - 1.76x slower
bigger deep_dup: 2413.1 i/s - 11.50x slower
require 'benchmark/ips'
require 'active_support/core_ext/object/deep_dup'
def quux(hash_arg)
hash_arg = hash_arg.dup
10.times { |i| hash_arg.merge!({ "num_#{i}" => i }) }
end
def deep_quux(hash_arg)
hash_arg = hash_arg.deep_dup
10.times { |i| hash_arg.merge!({ "num_#{i}" => i }) }
end
def corge(hash_arg)
hash_arg = Hash[hash_arg]
10.times { |i| hash_arg.merge!({ "num_#{i}" => i }) }
end
Benchmark.ips do |x|
x.report("dup") { quux({a: {b: 1}}) }
x.report("deep_dup") { deep_quux({a: {b: 1}}) }
x.report("Hash[]") { corge({a: {b: 1}}) }
x.compare!
end
def test_hash
hash = {}
100.times { |i| hash[i.to_s] = {"#{i}" => i } }
hash
end
hash1 = test_hash
hash2 = test_hash
hash3 = test_hash
Benchmark.ips do |x|
x.report("bigger dup") { quux(hash1) }
x.report("bigger deep_dup") { deep_quux(hash2) }
x.report("bigger Hash[]") { corge(hash3) }
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment