From a30498ba42a3b265e31889d80f0fbd22ee2eff3b Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Wed, 22 Oct 2014 01:27:45 -0700 Subject: [PATCH] Add benchmark for #flat_map MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Calculating ------------------------------------- .map.flatten with nested arrays 4718 i/100ms .flat_map with nested arrays 6048 i/100ms .map.flatten with no nested arrays 9804 i/100ms .flat_map with no nested arrays 9302 i/100ms ------------------------------------------------- .map.flatten with nested arrays 48118.3 (±4.8%) i/s - 240618 in 5.011942s .flat_map with nested arrays 63838.6 (±5.1%) i/s - 320544 in 5.034864s .map.flatten with no nested arrays 104879.3 (±4.4%) i/s - 529416 in 5.057802s .flat_map with no nested arrays 99935.3 (±6.6%) i/s - 502308 in 5.049506s --- benchmark/flat-map | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 benchmark/flat-map diff --git a/benchmark/flat-map b/benchmark/flat-map new file mode 100644 index 00000000..b10c5c9a --- /dev/null +++ b/benchmark/flat-map @@ -0,0 +1,17 @@ +require 'benchmark/ips' + +enum = (0..50).to_a +nested = enum.map { |i| [i] } + +def do_thing(blah) + blah * 1 +end + +Benchmark.ips do |x| + x.report('.map.flatten with nested arrays') { nested.map { |i| do_thing(i) }.flatten(1) } + x.report('.flat_map with nested arrays') { nested.flat_map { |i| do_thing(i) } } + + x.report('.map.flatten with no nested arrays') { enum.map { |i| do_thing(i) }.flatten(1) } + x.report('.flat_map with no nested arrays') { enum.flat_map { |i| do_thing(i) } } +end +