Created
April 11, 2016 23:42
-
-
Save bogdan/36187f53b1cd9d870572509e3c76b682 to your computer and use it in GitHub Desktop.
This file contains 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
require 'benchmark/ips' | |
require 'active_model' | |
# run this | |
# bundle exec ruby benchmark.rb && second_run=y bundle exec ruby benchmark.rb | |
class Person | |
include ActiveModel::Dirty | |
define_attribute_methods :name, :age | |
def initialize(name) | |
@name = name | |
end | |
def name=(val) | |
name_will_change! unless val == @name | |
@name = val | |
end | |
end | |
n = 2000000 | |
if ENV['second_run'] | |
module ActiveModel::Dirty | |
def attribute_changed?(attr, from: :none, to: :none) | |
result = changes_include?(attr) | |
result &&= from == __send__(attr) unless from == :none | |
result &&= to == changed_attributes[attr] unless to == :none | |
result | |
end | |
end | |
end | |
Benchmark.ips do |x| | |
x.hold! 'filename' | |
proc = Proc.new { | |
person = Person.new('tom') | |
person.name = 'bob' | |
n.times { | |
person.attribute_changed?(:name) | |
person.attribute_changed?(:age) | |
} | |
} | |
x.report("unchanged", &proc) | |
x.report("with nil", &proc) | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment