Skip to content

Instantly share code, notes, and snippets.

@spastorino
Created October 13, 2010 17:21
Show Gist options
  • Save spastorino/624479 to your computer and use it in GitHub Desktop.
Save spastorino/624479 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'sqlite3'
require 'benchmark'
require File.expand_path("../../../load_paths", __FILE__)
require 'active_record'
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3',
:database => ':memory:')
class User < ActiveRecord::Base
connection.create_table :users, :force => true do |t|
end
end
Benchmark.bm do |x|
x.report 'Model#find_by_id' do
100_000.times do
User.find_by_id(1)
end
end
end
# MRI 1.8.7
# without caching find_by_* methods
# user system total real
# Model#find_by_id 33.050000 1.060000 34.110000 ( 37.705276)
# caching find_by_* methods
# user system total real
# Model#find_by_id 29.550000 0.880000 30.430000 ( 31.797355)
# MRI 1.9.2
# without caching find_by_* methods
# user system total real
# Model#find_by_id 25.250000 0.170000 25.420000 ( 27.110316)
# caching find_by_* methods
# user system total real
# Model#find_by_id 23.190000 0.140000 23.330000 ( 24.353884)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment