Skip to content

Instantly share code, notes, and snippets.

@smathy
Last active April 5, 2016 19:04
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 smathy/5f9e93d5341e97db015f5f4e337259f3 to your computer and use it in GitHub Desktop.
Save smathy/5f9e93d5341e97db015f5f4e337259f3 to your computer and use it in GitHub Desktop.
[64] pry(main)> def run c, n=1000
[64] pry(main)* ActiveRecord::Base.logger = nil
[64] pry(main)* Foo.connection.uncached do
[64] pry(main)* Benchmark.bm(10) do |b|
[64] pry(main)* b.report(:exception) { n.times do; Foo.find(rand(1..c)) rescue nil; end }
[64] pry(main)* b.report(:find_by) { n.times do; Foo.find_by(id: rand(1..c)); end }
[64] pry(main)* end
[64] pry(main)* end
[64] pry(main)* end
=> :run
[65] pry(main)> run ( Foo.count * 1.05 ).round;
user system total real
exception 0.320000 0.010000 0.330000 ( 0.395392)
find_by 0.310000 0.020000 0.330000 ( 0.383363)
[66] pry(main)> run ( Foo.count * 2 ).round;
user system total real
exception 0.330000 0.020000 0.350000 ( 0.396790)
find_by 0.290000 0.010000 0.300000 ( 0.358287)
[67] pry(main)> run ( Foo.count * 100 ).round;
user system total real
exception 0.320000 0.020000 0.340000 ( 0.390607)
find_by 0.300000 0.010000 0.310000 ( 0.363125)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment