Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
require 'benchmark'
Benchmark.bmbm do |x|
x.report("Push x2") do
ary = []
100000.times do
ary << 'hi'
ary << 'hihi'
end
end
x.report("Concat ary") do
ary = []
100000.times do
ary += ['hi', 'hihi']
end
end
end
# Ruby 2.1.2
$ ruby ./array_benchmarks.rb
Rehearsal ----------------------------------------------
Push x2 0.030000 0.010000 0.040000 ( 0.040183)
Concat ary 20.480000 19.230000 39.710000 ( 39.916032)
------------------------------------ total: 39.750000sec
user system total real
Push x2 0.030000 0.000000 0.030000 ( 0.026020)
Concat ary 19.810000 18.560000 38.370000 ( 38.427382)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.