Skip to content

Instantly share code, notes, and snippets.

@lucianosousa
Created January 15, 2016 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucianosousa/5f188167b1f907e0f34f to your computer and use it in GitHub Desktop.
Save lucianosousa/5f188167b1f907e0f34f to your computer and use it in GitHub Desktop.
require 'active_support'
require 'active_record'
require 'benchmark/ips'
ActiveRecord::Base.establish_connection(
adapter: 'mysql2',
encoding: 'utf8',
reconnect: true,
pool: 25,
host: 'localhost',
database: 'test_database',
username: 'root',
password: ''
)
class Customer < ActiveRecord::Base; end
name = 'Luciano Sousa'
Benchmark.ips do |x|
x.report('where_first') { Customer.where(name: name).first }
x.report('find_by_name') { Customer.find_by_name(name) }
x.report('find_by') { Customer.find_by(name: name) }
x.compare!
end
@lucianosousa
Copy link
Author

$ ruby benchmark1.rb
Calculating -------------------------------------
         where_first   130.000  i/100ms
        find_by_name   223.000  i/100ms
             find_by   224.000  i/100ms
-------------------------------------------------
         where_first      1.400k (± 6.5%) i/s -      7.020k
        find_by_name      2.304k (± 6.9%) i/s -     11.596k
             find_by      2.330k (± 7.3%) i/s -     11.648k

Comparison:
             find_by:     2330.4 i/s
        find_by_name:     2303.5 i/s - 1.01x slower
         where_first:     1399.6 i/s - 1.67x slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment