Created
August 4, 2017 20:27
-
-
Save eregon/2b2e10592c500ee8e5321e7705976d4e 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.iterations = 5 | |
x.report '#to_proc include?' do | |
array_of_numbers.map &[range, :include?] | |
end | |
x.report 'block include?' 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.iterations = 5 | |
x.report '#to_proc append' do | |
array_of_arrays.map &[:+, array_to_append] | |
end | |
x.report 'block append' do | |
array_of_arrays.map { |array| array + array_to_append } | |
end | |
x.compare! | |
end |
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 | |
MY_RANGE = 20..40_000 | |
Benchmark.ips do |x| | |
x.iterations = 5 | |
x.report '#to_proc include?' do | |
array_of_numbers.map &[MY_RANGE, :include?] | |
end | |
x.report 'block include?' do | |
array_of_numbers.map { |number| MY_RANGE.include? number } | |
end | |
x.compare! | |
end | |
array_of_arrays = [[:a],[:b],[:c],[:d]] | |
ARRAY_TO_APPEND = [1,2] | |
Benchmark.ips do |x| | |
x.iterations = 5 | |
x.report '#to_proc append' do | |
array_of_arrays.map &[:+, ARRAY_TO_APPEND] | |
end | |
x.report 'block append' 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