From fcf55f1b1c6f42e633978b23c06d1d1e5d07c7d1 Mon Sep 17 00:00:00 2001 From: Carlos Contreras Date: Fri, 2 Jun 2017 16:18:14 -0500 Subject: [PATCH] Add double splat operator vs merge --- README.md | 17 +++++++++++++++++ code/hash/merge-vs-double-splat-operator.rb | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 code/hash/merge-vs-double-splat-operator.rb diff --git a/README.md b/README.md index 81f1b44..fd036e9 100644 --- a/README.md +++ b/README.md @@ -719,6 +719,23 @@ Comparison: Hash#merge!: 10653.3 i/s - 2.66x slower ``` +##### `Hash#merge` vs `Hash#**other` [code](code/hash/merge-vs-double-splat-operator.rb) + +``` +$ ruby -v merge-vs-double-splat-operator.rb +ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin15] +Warming up -------------------------------------- + Hash#**other 64.624k i/100ms + Hash#merge 38.827k i/100ms +Calculating ------------------------------------- + Hash#**other 798.397k (± 6.9%) i/s - 4.007M in 5.053516s + Hash#merge 434.171k (± 4.5%) i/s - 2.174M in 5.018927s + +Comparison: + Hash#**other: 798396.6 i/s + Hash#merge: 434170.8 i/s - 1.84x slower +``` + ##### `Hash#merge` vs `Hash#merge!` [code](code/hash/merge-vs-merge-bang.rb) ``` diff --git a/code/hash/merge-vs-double-splat-operator.rb b/code/hash/merge-vs-double-splat-operator.rb new file mode 100644 index 0000000..b0b63e0 --- /dev/null +++ b/code/hash/merge-vs-double-splat-operator.rb @@ -0,0 +1,17 @@ +require 'benchmark/ips' + +def fast + h2 = { a: 'a' } + { one: 1, **h2 } +end + +def slow + h2 = { a: 'a' } + { one: 1 }.merge(h2) +end + +Benchmark.ips do |x| + x.report('Hash#**other') { fast } + x.report('Hash#merge') { slow } + x.compare! +end