Created
August 4, 2017 15:11
-
-
Save ch1c0t/5efc24f849f66b67470e182e78acaebb to your computer and use it in GitHub Desktop.
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/ips' | |
class Array | |
def to_proc | |
if self[0].is_a? Symbol | |
method, *arguments = self | |
-> receiver { receiver.send method, *arguments } | |
elsif self[1].is_a? Symbol | |
receiver, method, *arguments = self | |
-> argument { receiver.send method, argument } | |
else | |
arguments = self | |
-> receiver { receiver.to_proc[*arguments] } | |
end | |
end | |
end | |
array_of_numbers = 1..1_000_000 | |
range = 20..40_000 | |
Benchmark.ips do |x| | |
x.report 'Array#to_proc' do | |
array_of_numbers.map &[range, :include?] | |
end | |
x.report 'default' do | |
array_of_numbers.map { |number| range.include? number } | |
end | |
x.compare! | |
end | |
array_of_arrays = [[:a],[:b],[:c],[:d]] | |
array_to_append = [1,2] | |
Benchmark.ips do |x| | |
x.report 'Array#to_proc' do | |
array_of_arrays.map &[:+, array_to_append] | |
end | |
x.report 'default' do | |
array_of_arrays.map { |array| array + array_to_append } | |
end | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment