Skip to content

Instantly share code, notes, and snippets.

@goldeneggg
Last active May 19, 2021 01:26
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 goldeneggg/922b4acf5cb242afc614348e6c368f50 to your computer and use it in GitHub Desktop.
Save goldeneggg/922b4acf5cb242afc614348e6c368f50 to your computer and use it in GitHub Desktop.
Compare performance `Hash#merge` and `Hash#merge!` for Ruby 2.6
require 'benchmark/ips'
require 'benchmark/memory'
class Hoge
class << self
DEFAULT_TIME = 5
DEFAULT_WARMUP = 2
def ips(title:,
time: DEFAULT_TIME,
warmup: DEFAULT_WARMUP,
suite: nil,
&block)
Benchmark.ips do |x|
x.time = time
x.warmup = warmup
x.config(suite: suite) unless suite.nil?
x.report(title) { block.call }
end
end
def memory(title:, &block)
Benchmark.memory do |x|
x.report(title) { block.call }
end
end
def ips_compare(procs:,
time: DEFAULT_TIME,
warmup: DEFAULT_WARMUP,
suite: nil,
result_holder: nil,
result_saver: nil)
Benchmark.ips do |x|
x.time = time
x.warmup = warmup
x.config(suite: suite) unless suite.nil?
procs.each do |p|
x.report(p[:title]) { p[:proc].call }
end
x.hold!(result_holder) unless result_holder.nil?
x.save!(result_saver) unless result_saver.nil?
x.compare!
end
end
def memory_compare(procs:, result_holder: nil)
Benchmark.memory do |x|
procs.each do |p|
x.report(p[:title]) { p[:proc].call }
end
x.hold!(result_holder) unless result_holder.nil?
x.compare!
end
end
end
end
PROCS = [
{
title: 'merge',
proc: Proc.new do
h = { hoge: 'hogehoge' }
h.merge({ huga: 'hugahugahuga' })
end
},
{
title: 'merge!',
proc: Proc.new do
h = { hoge: 'hogehoge' }
h.merge!({ huga: 'hugahugahuga' })
end
},
]
Hoge.ips_compare(procs: PROCS)
Hoge.memory_compare(procs: PROCS)
@goldeneggg
Copy link
Author

goldeneggg commented May 19, 2021

 % ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin19]

% ruby merge_vs_mergex.rb
Warming up --------------------------------------
               merge   307.079k i/100ms
              merge!   355.438k i/100ms
Calculating -------------------------------------
               merge      3.054M (± 1.3%) i/s -     15.354M in   5.029121s
              merge!      3.605M (± 1.2%) i/s -     18.127M in   5.028477s

Comparison:
              merge!:  3605480.7 i/s
               merge:  3053507.7 i/s - 1.18x  (± 0.00) slower

Calculating -------------------------------------
               merge   776.000  memsize (     0.000  retained)
                         5.000  objects (     0.000  retained)
                         2.000  strings (     0.000  retained)
              merge!   544.000  memsize (     0.000  retained)
                         4.000  objects (     0.000  retained)
                         2.000  strings (     0.000  retained)

Comparison:
              merge!:        544 allocated
               merge:        776 allocated - 1.43x more

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