Skip to content

Instantly share code, notes, and snippets.

@mdespuits
Created April 3, 2013 20:44
Show Gist options
  • Save mdespuits/5305111 to your computer and use it in GitHub Desktop.
Save mdespuits/5305111 to your computer and use it in GitHub Desktop.
Array concatenation benchmarks
require 'benchmark'
ITERATIONS = 10_000
Benchmark.bm do |bm|
bm.report("#<< -> #flatten!: ") do
array = [1,2,3]
ITERATIONS.times do
array << [4,5,6]
array.flatten!
end
end
bm.report("+=: ") do
array = [1,2,3]
ITERATIONS.times do
array += [4,5,6]
end
end
bm.report("#concat: ") do
array = [1,2,3]
ITERATIONS.times do
array.concat [4,5,6]
end
end
end
# OUTCOME
#
# => user system total real
# => #<< -> #flatten!: 8.280000 0.200000 8.480000 ( 8.473539)
# => +=: 0.240000 0.350000 0.590000 ( 0.599704)
# => #concat: 0.000000 0.000000 0.000000 ( 0.001990)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment