Skip to content

Instantly share code, notes, and snippets.

@mohnish
Last active February 26, 2016 20:16
Show Gist options
  • Select an option

  • Save mohnish/d51e6247ed42d61201c6 to your computer and use it in GitHub Desktop.

Select an option

Save mohnish/d51e6247ed42d61201c6 to your computer and use it in GitHub Desktop.
benchmark merge_vs_square_accessor
require 'benchmark/ips'
foo = { foo_1: 'one', foo_2: 'two' }
bar = { bar_1: 'one', bar_2: 'two' }
Benchmark.ips do |x|
x.config(:time => 5, :warmup => 2)
x.report('merge!') do |times|
foo = { foo_1: 'one', foo_2: 'two' }
bar = { bar_1: 'one', bar_2: 'two' }
foo.merge!(bar)
end
x.report('square_accessor') do |times|
foo = { foo_1: 'one', foo_2: 'two' }
bar = { bar_1: 'one', bar_2: 'two' }
foo[:bar_1] = 'one'
foo[:bar_2] = 'two'
end
x.report('merge') do |times|
foo = { foo_1: 'one', foo_2: 'two' }
bar = { bar_1: 'one', bar_2: 'two' }
foo.merge(bar)
end
x.compare!
end
@mohnish

mohnish commented Feb 26, 2016

Copy link
Copy Markdown
Author

results:

ruby merge_vs_merge_bang_vs_square_accessor.rb
Warming up --------------------------------------
              merge!    52.247k i/100ms
     square_accessor    50.111k i/100ms
               merge    32.190k i/100ms
Calculating -------------------------------------
              merge!     46.961B (±31.4%) i/s -     79.139B
     square_accessor     45.220B (±31.7%) i/s -     76.270B
               merge     17.328B (±43.9%) i/s -     35.860B

Comparison:
              merge!: 46961394641.1 i/s
     square_accessor: 45220484056.7 i/s - same-ish: difference falls within error
               merge: 17328443767.5 i/s - 2.71x slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment