Skip to content

Instantly share code, notes, and snippets.

@keymastervn
Created February 22, 2022 08:08
Show Gist options
  • Save keymastervn/02e766b2612269d36592ec3c7ab759a0 to your computer and use it in GitHub Desktop.
Save keymastervn/02e766b2612269d36592ec3c7ab759a0 to your computer and use it in GitHub Desktop.
benchmark activerecord mem / ips find_in_batches
require 'benchmark'
require 'benchmark/ips'
require 'benchmark/memory'
def select
Member.select(:id).find_in_batches(batch_size: 10) do |ids|
puts ids.map(&:id).join(',')
end
end
def pluck
Member.find_in_batches(batch_size: 10) do |ids|
puts ids.pluck(:id).join(',')
end
end
Member.uncached do
# bm elapsed time
Benchmark.bmbm do |x|
x.report('select') { send :select }
x.report('pluck') { send :pluck }
end
Benchmark.memory do |x|
x.report('select') { send :select }
x.report('pluck') { send :pluck }
x.compare!
end
Benchmark.ips do |x|
x.report('select') { send :select }
x.report('pluck') { send :pluck }
x.compare!
end
end
@keymastervn
Copy link
Author

Comparison:
select: 273040 allocated
pluck: 641345 allocated - 2.35x more

Comparison:
select: 115.4 i/s
pluck: 71.8 i/s - 1.61x (± 0.00) slower

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