Skip to content

Instantly share code, notes, and snippets.

@richdrich
Created July 19, 2020 21:59
Show Gist options
  • Save richdrich/cc3dd334a38946d1b47baa3f8af35e97 to your computer and use it in GitHub Desktop.
Save richdrich/cc3dd334a38946d1b47baa3f8af35e97 to your computer and use it in GitHub Desktop.
Test performance of to_h vs insert
require 'date'
NUM_ENTRIES = 100000
t = DateTime.now
h = (0..NUM_ENTRIES).each.map do |n|
[n, 25 * n]
end.to_h
puts "#{(DateTime.now - t).to_f * 1000 * 86400} ms to populate by to_h"
t = DateTime.now
h = {}
(0..NUM_ENTRIES).each do |n|
h[n] = 25 * n
end
puts "#{(DateTime.now - t).to_f * 1000 * 86400} ms to populate by insert"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment