Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@keichan34
Created June 2, 2014 02:27
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 keichan34/5c011dcd7f99d1a34e7c to your computer and use it in GitHub Desktop.
Save keichan34/5c011dcd7f99d1a34e7c to your computer and use it in GitHub Desktop.
#!/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