Skip to content

Instantly share code, notes, and snippets.

@yuroyoro
Last active August 29, 2015 14:19
Show Gist options
  • Save yuroyoro/4953e7b7e57479b844d6 to your computer and use it in GitHub Desktop.
Save yuroyoro/4953e7b7e57479b844d6 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
begin
class CreateModels < ActiveRecord::Migration
def change
create_table :books do |t|
t.string :attribute1
t.integer :attribute2
t.float :attribute3
t.boolean :attribute4
t.text :attribute5
t.datetime :attribute6
t.integer :attribute7, :limit => 8
t.timestamps null: false
end
end
end
ActiveRecord::Migration.verbose = false
CreateModels.new.change
rescue => e
# Tables already exist
# puts e.message
end
class Book < ActiveRecord::Base
if Rails.version.split(".").first.to_i < 4
attr_accessible :attribute1, :attribute2, :attribute3, :attribute4, :attribute5, :attribute6, :attribute7
end
end
unless Book.count >= 1000
# insert 1000 records via seeds
1000.times do
Book.create!(
attribute1: 'foo',
attribute2: 12345,
attribute3: 987.65,
attribute4: true,
attribute5: "TEXTTEXTTEXTTEXT",
attribute6: Time.now,
attribute7: 999999999999,
)
end
end
books1 = Book.all.to_a
books2 = Book.all.to_a
puts "=" * 80
puts "Benchmark : ActiveRecord read by compiled attribute method"
puts "#{Rails.version} - #{ActiveRecord::Base.configurations[Rails.env]['adapter']}"
puts "=" * 80
puts ""
Benchmark.ips do |x|
x.report('compiled method') do
books1.each do |book|
book.id
book.attribute1
book.attribute2
book.attribute3
book.attribute4
book.attribute5
book.attribute6
book.attribute7
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment