Created
August 30, 2016 15:51
-
-
Save cheeyeo/05d86458f71b56eebb29c3ad11b436a3 to your computer and use it in GitHub Desktop.
Benchmark to_proc vs block in Enumerable#map
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
| #/usr/bin/env ruby | |
| require "benchmark/ips" | |
| arr = [1.0, 2.0, 3.0] | |
| h = { foo: 1, bar: 2, baz: 3 } | |
| class Double | |
| def to_proc | |
| proc{ |n| n * 2 } | |
| end | |
| end | |
| Benchmark.ips do |x| | |
| x.report("arr.map{ |x| x*2 }"){ | |
| arr.map{ |x| x*2 } | |
| } | |
| x.report("arr.map(&Double.new)"){ | |
| arr.map(&Double.new) | |
| } | |
| x.report("[:foo, :bar].map"){ | |
| [:foo, :bar].map{ |x| h[x] } | |
| } | |
| x.report("[:foo, :bar].map(&h)"){ | |
| [:foo, :bar].map(&h) | |
| } | |
| x.compare! | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment