Skip to content

Instantly share code, notes, and snippets.

@mohnish
Last active June 10, 2016 22:13
Show Gist options
  • Select an option

  • Save mohnish/8eaeae84609481fae233 to your computer and use it in GitHub Desktop.

Select an option

Save mohnish/8eaeae84609481fae233 to your computer and use it in GitHub Desktop.
benchmark for find in batches
require 'benchmark/ips'
Benchmark.ips do |x|
x.config(:time => 5, :warmup => 2)
x.report("select in batches and lookup using the id") do
User.find_in_batches(:conditions => ["updated_at > ?", 8.weeks.ago.to_s(:db)], :select => :id) do |users|
users.map { |user| User.find(user.id, :include => [:user_metric]).id }
end
end
x.report("lookup in batches") do |times|
User.find_in_batches(:conditions => ["updated_at > ?", 8.weeks.ago.to_s(:db)], :include => [:user_metric]) do |users|
users.map { |user| user.id }
end
end
x.compare!
end
Calculating -------------------------------------
select in batches and lookup using the id
20.000 i/100ms
lookup in batches 36.000 i/100ms
-------------------------------------------------
select in batches and lookup using the id
266.158 (±39.8%) i/s - 1.060k
lookup in batches 21.686k (±12.7%) i/s - 64.728k
Comparison:
lookup in batches: 21686.5 i/s
select in batches and lookup using the id: 266.2 i/s - 81.48x slower
@mohnish

mohnish commented Dec 11, 2015

Copy link
Copy Markdown
Author

Installation

gem install benchmark-ips

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