Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
2 contributors

Users who have contributed to this file

@ShPakvel @JuanitoFatas
23 lines (17 sloc) 413 Bytes
require 'benchmark/ips'
ARRAY = (1..100).to_a
def slow_flatten_1
ARRAY.map { |e| [e, e] }.flatten(1)
end
def slow_flatten
ARRAY.map { |e| [e, e] }.flatten
end
def fast
ARRAY.flat_map { |e| [e, e] }
end
Benchmark.ips do |x|
x.report('Array#map.flatten(1)') { slow_flatten_1 }
x.report('Array#map.flatten') { slow_flatten }
x.report('Array#flat_map') { fast }
x.compare!
end
You can’t perform that action at this time.