Created
January 14, 2015 15:06
-
-
Save quanon/9f3886b803d0f99ac35d to your computer and use it in GitHub Desktop.
flat_map はやい!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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