Created
October 31, 2013 22:57
-
-
Save chulkilee/7258572 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' | |
big_number = 1_000_000 | |
a = 1.upto big_number | |
b = big_number.upto big_number * 2 | |
a_array = a.to_a | |
b_array = b.to_a | |
class MultipleEnumerator | |
include Enumerable | |
def initialize(*args) | |
@data = *args | |
end | |
def each(&block) | |
@data.each do |data| | |
data.each(&block) | |
end | |
end | |
end | |
Benchmark.bm do |x| | |
x.report 'MultipleEnumerator' do | |
e = MultipleEnumerator.new(a, b) | |
puts e.reduce :+ | |
end | |
x.report 'to_a.flatten' do | |
puts [a, b].map(&:to_a).flatten.reduce :+ | |
end | |
x.report 'flatten' do | |
puts [a_array, b_array].flatten.reduce :+ | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment