Skip to content

Instantly share code, notes, and snippets.

@jaynetics
Created February 27, 2023 10:52
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 jaynetics/c99d7f52bfafe2ff434a870721c067d4 to your computer and use it in GitHub Desktop.
Save jaynetics/c99d7f52bfafe2ff434a870721c067d4 to your computer and use it in GitHub Desktop.
Ruby benchmark for making empty, compare_by_identity Hash with new / literal vs dup
# results on ruby 3.2:
#
# Warming up --------------------------------------
# new 277.194k i/100ms
# dup 394.839k i/100ms
# Calculating -------------------------------------
# new 2.754M (± 1.9%) i/s - 13.860M in 5.034959s
# dup 4.006M (± 0.8%) i/s - 20.137M in 5.026561s
#
# Comparison:
# dup: 4006345.3 i/s
# new: 2753695.8 i/s - 1.45x (± 0.00) slower
require 'benchmark/ips'
H = {}.tap(&:compare_by_identity)
Benchmark.ips do |x|
x.report('new') { {}.tap(&:compare_by_identity) }
x.report('dup') { H.dup }
x.compare!
end; nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment