Files
jekyll/benchmark/flat-map
fen 7729d12cdc include a hashbang for all benchmark scripts
this also makes every benchmark script executable
2016-10-22 03:13:30 +02:00

18 lines
515 B
Ruby
Executable File

#!/usr/bin/env ruby
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