Skip to content

Instantly share code, notes, and snippets.

@giannafusaro
Last active June 27, 2017 20:08
Show Gist options
  • Save giannafusaro/9d5d14d2f4a1649176d4209f3a5d438b to your computer and use it in GitHub Desktop.
Save giannafusaro/9d5d14d2f4a1649176d4209f3a5d438b to your computer and use it in GitHub Desktop.
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'benchmark.db'
)
# ActiveRecord::Migration.class_eval do
# create_table :users do |t|
# t.string :email
# t.string :name
# end
# end
class User < ActiveRecord::Base
attr_accessor :email, :name
end
def tracerpoint
calls = []
trace = TracePoint.new(:call, :c_call) do |tp|
calls << [tp.defined_class, tp.method_id, tp.lineno]
end
trace.enable
yield
trace.disable
calls.group_by(&:itself).map {|k, v| {k => v.length}}.sort_by {|h| -h.values.first}
end
@user = User.find_or_create_by(email: 'gianna@gianna.codes', name: 'gianna')
calls = tracerpoint do
@user.present?
end
# puts calls
puts "count for @user.present?: #{calls.collect(&:values).flatten.sum}"
calls = tracerpoint do
!!@user
end
puts "count for !!@user: #{calls.collect(&:values).flatten.sum}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment