Skip to content

Instantly share code, notes, and snippets.

@DmytroVasin
Last active June 17, 2018 11:58
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 DmytroVasin/666b6ef191161160d258982014ad5e63 to your computer and use it in GitHub Desktop.
Save DmytroVasin/666b6ef191161160d258982014ad5e63 to your computer and use it in GitHub Desktop.
Time to add a new key/value pair (ms)
# Ruby -v: 2.3.4
# Chart: https://jsfiddle.net/81ef590x/116/
require 'benchmark'
def without_gc
GC.start
GC.disable
yield
GC.enable
end
170.times do |size|
without_gc do
hashes = []
100000.times do
hash = {}
(1..size).each do
hash[rand] = rand
end
hashes << hash
end
Benchmark.bm do |bench|
bench.report("adding element number #{size+1}") do
100000.times do |n|
hashes[n][size] = rand
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment