Skip to content

Instantly share code, notes, and snippets.

@spickermann
Created February 17, 2014 23:12
Show Gist options
  • Save spickermann/9061186 to your computer and use it in GitHub Desktop.
Save spickermann/9061186 to your computer and use it in GitHub Desktop.
def array_unshift
foo = [:foo, :bar, :bar]
foo.unshift(:something)
end
def array_concat
foo = [:something]
foo.concat([:foo, :bar, :bar])
end
require 'benchmark'
n = 10_000_000
Benchmark.bmbm(15) do |x|
x.report("array_unshift:") { n.times do; array_unshift; end }
x.report("array_concat:") { n.times do; array_concat; end }
end
# Rehearsal ---------------------------------------------------
# array_unshift: 2.890000 0.030000 2.920000 ( 2.913567)
# array_concat: 3.380000 0.000000 3.380000 ( 3.384986)
# ------------------------------------------ total: 6.300000sec
# user system total real
# array_unshift: 2.870000 0.020000 2.890000 ( 2.898327)
# array_concat: 3.420000 0.000000 3.420000 ( 3.415004)
@spickermann
Copy link
Author

Conclusion: It does not really matter...

@johnsyweb
Copy link

👍

@pablolee
Copy link

Very interesting results!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment