Skip to content

Instantly share code, notes, and snippets.

@quanon
Created January 14, 2015 15:06
Show Gist options
  • Save quanon/9f3886b803d0f99ac35d to your computer and use it in GitHub Desktop.
Save quanon/9f3886b803d0f99ac35d to your computer and use it in GitHub Desktop.
flat_map はやい!
require 'benchmark'
array = (1..10_000).map { |n| [n] }
Benchmark.bm do |x|
x.report('map & flatten:') { array.map { |a| a << 'yuno' }.flatten }
x.report('map & flatten!:') { array.map { |a| a << 'yuno' }.flatten! }
x.report('flat_map:') { array.flat_map { |a| a << 'yuno' } }
end
# user system total real
# map & flatten: 0.010000 0.000000 0.010000 ( 0.008364)
# map & flatten!: 0.000000 0.000000 0.000000 ( 0.007843)
# flat_map: 0.010000 0.010000 0.020000 ( 0.002672)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment