Skip to content

Instantly share code, notes, and snippets.

@coreyward
Created February 16, 2011 22:17
Show Gist options
  • Select an option

  • Save coreyward/830384 to your computer and use it in GitHub Desktop.

Select an option

Save coreyward/830384 to your computer and use it in GitHub Desktop.
A quick benchmark to confirm/dispel performance concerns over using `.map(&:to_i)` instead of a literal block (`.map { |x| x.to_i }`).
require 'benchmark'
n = 100000
a = %w{ 1 2 3 4 5 6 7 8 9 0 }
Benchmark.bmbm do |b|
b.report 'Explicit Block' do
n.times do
a.map { |x| x.to_i }
end
end
b.report 'Symbol#to_proc' do
n.times do
a.map &:to_i
end
end
end
# Rehearsal --------------------------------------------------
# Explicit Block   0.350000   0.000000   0.350000 (  0.371145)
# Symbol#to_proc   0.730000   0.020000   0.750000 (  0.784472)
# ----------------------------------------- total: 1.100000sec
#
#                      user     system      total        real
# Explicit Block   0.360000   0.010000   0.370000 (  0.365922)
# Symbol#to_proc   0.730000   0.020000   0.750000 (  0.786993)
@coreyward
Copy link
Copy Markdown
Author

Using MRI Ruby 2.0.0:

Rehearsal --------------------------------------------------
Explicit Block   0.160000   0.000000   0.160000 (  0.159286)
Symbol#to_proc   0.150000   0.000000   0.150000 (  0.148407)
----------------------------------------- total: 0.310000sec

                     user     system      total        real
Explicit Block   0.150000   0.000000   0.150000 (  0.157774)
Symbol#to_proc   0.150000   0.000000   0.150000 (  0.149271)

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