Skip to content

Instantly share code, notes, and snippets.

@goshacmd
Last active December 10, 2015 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goshacmd/4435213 to your computer and use it in GitHub Desktop.
Save goshacmd/4435213 to your computer and use it in GitHub Desktop.
Array() vs #flatten
require 'benchmark'
n = 1_000_000
Benchmark.bm do |x|
x.report('[[1]]') { n.times { [[1]].flatten } }
x.report('Array([1])') { n.times { Array([1]) } }
x.report('[1]') { n.times { [1].flatten } }
x.report('Array(1)') { n.times { Array(1) } }
end
# user system total real
# [[1]] 1.470000 0.020000 1.490000 ( 1.493567)
# Array([1]) 0.280000 0.000000 0.280000 ( 0.276987)
# [1] 1.010000 0.020000 1.030000 ( 1.037248)
# Array(1) 0.370000 0.000000 0.370000 ( 0.369389)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment