Created
February 16, 2011 22:17
-
-
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 }`).
This file contains hidden or 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' | |
| 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) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using MRI Ruby 2.0.0: