Created
June 18, 2011 17:02
-
-
Save ferrous26/1033278 to your computer and use it in GitHub Desktop.
Compare giving a block versus giving proc created using Method#to_proc
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 'rubygems' | |
| gem 'minitest', '>= 2.2' | |
| require 'minitest/autorun' | |
| require 'minitest/benchmark' | |
| class BenchMethodToProc < MiniTest::Unit::TestCase | |
| def self.test_order | |
| :alpha | |
| end | |
| def self.bench_range | |
| bench_exp 10_000, 1_000_000 | |
| end | |
| DATA = [1, 2, 3, 'one', 'two', 'three', :one, :two, :three] | |
| def bench_block | |
| assert_performance_linear do |n| | |
| n.times do DATA.map { |x| x.class } end | |
| end | |
| end | |
| def my_block x | |
| x.class | |
| end | |
| def bench_method_to_proc | |
| assert_performance_linear do |n| | |
| n.times do DATA.map &method(:my_block) end | |
| end | |
| end | |
| end |
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
| # Running benchmarks: | |
| BenchMethodToProc 10000 100000 1000000 | |
| bench_block 0.039517 0.307440 3.282183 | |
| bench_method_to_proc 0.150314 1.416282 14.335822 | |
| Finished benchmarks in 19.725056s, 0.1014 tests/s, 0.1014 assertions/s. | |
| 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment