Skip to content

Instantly share code, notes, and snippets.

@schneems
Last active May 13, 2019
Embed
What would you like to do?
# frozen_string_literal: true
require "active_record"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.string :name, :email, :username, :password, :birthday, :foo, :bar, :baz, :dog_name
t.timestamps null: false
end
create_table :topics, force: true do |t|
t.string :title
t.integer :user_id
t.timestamps null: false
end
end
class Topic < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :topics
end
user = User.create!(name: "user name", dog_name: "cinco")
user.respond_to?(:name)
topic = user.topics.build(title: "topic title")
# Benchmark.ips do |x|
# x.report("changed?") { topic.changed? }
# x.report("changed") { topic.changed }
# x.report("changes") { topic.changes }
# x.report("changed_attributes") { topic.changed_attributes }
# x.report("title_change") { topic.title_change }
# x.report("title_was") { topic.title_was }
# end
Benchmark.bm { |x|
x.report("respond_to") {
1_000_000.times {
user.respond_to?(:dog_name)
}
}
}
respond_to 1.067132 0.000808 1.067940 ( 1.068390)
respond_to 1.241908 0.016600 1.258508 ( 1.285221)
respond_to 1.207405 0.005271 1.212676 ( 1.217845)
respond_to 1.184683 0.003760 1.188443 ( 1.191192)
respond_to 1.070051 0.002091 1.072142 ( 1.073820)
respond_to 1.135280 0.005627 1.140907 ( 1.149822)
respond_to 1.087188 0.004278 1.091466 ( 1.095548)
respond_to 1.055867 0.001639 1.057506 ( 1.058680)
respond_to 1.016671 0.001528 1.018199 ( 1.019871)
respond_to 1.043922 0.001511 1.045433 ( 1.046850)
respond_to 1.047997 0.001400 1.049397 ( 1.050332)
respond_to 1.038043 0.000994 1.039037 ( 1.039547)
respond_to 1.021775 0.001520 1.023295 ( 1.024422)
respond_to 1.070376 0.002899 1.073275 ( 1.075764)
respond_to 1.108423 0.002648 1.111071 ( 1.115584)
respond_to 1.220066 0.003965 1.224031 ( 1.226699)
respond_to 1.061367 0.002085 1.063452 ( 1.065009)
respond_to 1.060796 0.002615 1.063411 ( 1.065675)
respond_to 1.050530 0.001080 1.051610 ( 1.052452)
respond_to 1.106280 0.003030 1.109310 ( 1.112513)
respond_to 0.738569 0.000581 0.739150 ( 0.739610)
respond_to 0.737943 0.000476 0.738419 ( 0.738887)
respond_to 0.752631 0.003258 0.755889 ( 0.758984)
respond_to 0.768799 0.001314 0.770113 ( 0.773280)
respond_to 0.748439 0.001468 0.749907 ( 0.750912)
respond_to 0.742347 0.000922 0.743269 ( 0.743870)
respond_to 0.732145 0.001841 0.733986 ( 0.735342)
respond_to 0.798340 0.001562 0.799902 ( 0.801558)
respond_to 0.733198 0.000566 0.733764 ( 0.734220)
respond_to 0.705524 0.000705 0.706229 ( 0.706727)
respond_to 0.729143 0.000631 0.729774 ( 0.730290)
respond_to 0.733833 0.000681 0.734514 ( 0.734881)
respond_to 0.853904 0.001371 0.855275 ( 0.856204)
respond_to 0.729841 0.001577 0.731418 ( 0.732504)
respond_to 0.720388 0.000809 0.721197 ( 0.721655)
respond_to 0.879052 0.001410 0.880462 ( 0.881244)
respond_to 0.748783 0.000290 0.749073 ( 0.749206)
respond_to 0.743012 0.001387 0.744399 ( 0.745671)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment