Created
February 22, 2022 08:08
-
-
Save keymastervn/02e766b2612269d36592ec3c7ab759a0 to your computer and use it in GitHub Desktop.
benchmark activerecord mem / ips find_in_batches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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