Skip to content

Instantly share code, notes, and snippets.

@chulkilee
Created October 31, 2013 22:57
Show Gist options
  • Save chulkilee/7258572 to your computer and use it in GitHub Desktop.
Save chulkilee/7258572 to your computer and use it in GitHub Desktop.
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