Last active
April 25, 2016 09:54
-
-
Save fabien-michel/202a5a9ae2ccb9f6889f55858f6806fc to your computer and use it in GitHub Desktop.
test merge! on hstore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BenchMerge | |
# class CreateAnimals < ActiveRecord::Migration | |
# def change | |
# enable_extension 'hstore' | |
# | |
# | |
# create_table :animals do |t| | |
# t.hstore :data | |
# t.timestamps null: false | |
# end | |
# end | |
# end | |
def self.perf | |
h = (1..26).map{|i| [(96+i).chr.to_sym, rand(100.0)]}.to_h | |
Benchmark.bm do |x| | |
x.report(:merge) do | |
animal = Animal.new(data:{}) | |
3000.times do | |
animal.data = animal.data.merge h | |
end | |
end | |
x.report(:merge!) do | |
animal = Animal.new(data:{}) | |
3000.times do | |
animal.data.merge! h | |
end | |
end | |
end | |
# user system total real | |
# merge 3.640000 0.020000 3.660000 ( 3.728977) | |
# merge! 0.000000 0.000000 0.000000 ( 0.005567) | |
end | |
def self.dirty | |
h = {a:"1", b:"2"} | |
puts "1. merge" | |
animal = Animal.create!(data: h) | |
animal.data = animal.data.merge h | |
puts animal.changed? ? "changed" : "not changed" | |
puts animal.changes.inspect | |
puts "2. merge!" | |
animal = Animal.create!(data: h) | |
animal.data.merge! h | |
puts animal.changed? ? "changed" : "not changed" | |
puts animal.changes.inspect | |
# 1. merge | |
# not changed | |
# {} | |
# 2. merge! | |
# changed | |
# {"data"=>[{"a"=>"1", "b"=>"2"}, {"a"=>"1", "b"=>"2"}]} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment