Skip to content

Instantly share code, notes, and snippets.

@korun
Last active August 29, 2015 14:18
Show Gist options
  • Save korun/1a9a1a42338cb4edfda1 to your computer and use it in GitHub Desktop.
Save korun/1a9a1a42338cb4edfda1 to your computer and use it in GitHub Desktop.
require 'benchmark'
n = 1_000_000
login = 'login'
time = Time.now.to_i
Benchmark.bmbm 20 do |results|
results.report 'cycle' do
n.times do
end
end
# result: 3 array object usage
results.report '[] + []' do
n.times do
[login, time] + [3, 4, 5]
end
end
# result: 2 array object usage
results.report '[].push(*[])' do
n.times do
[login, time].push(*[3, 4, 5])
end
end
# result: 1 array object usage
results.report '[].unshift(a, b)' do
n.times do
[3, 4, 5].unshift(login, time)
end
end
end
@korun
Copy link
Author

korun commented Apr 2, 2015

$ uname -a && ruby benchmark.rb
Linux Inspiron-17R 3.13.0-43-generic #72-Ubuntu SMP Mon Dec 8 19:35:44 UTC 2014 i686 i686 i686 GNU/Linux
Rehearsal --------------------------------------------------------
cycle                  0.050000   0.000000   0.050000 (  0.047094)
[] + []                0.340000   0.000000   0.340000 (  0.338438)
[].push(*[])           0.360000   0.000000   0.360000 (  0.362075)
[].unshift(a, b)       0.270000   0.000000   0.270000 (  0.281735)
----------------------------------------------- total: 1.020000sec

                           user     system      total        real
cycle                  0.050000   0.000000   0.050000 (  0.046404)
[] + []                0.340000   0.000000   0.340000 (  0.343467)
[].push(*[])           0.360000   0.000000   0.360000 (  0.357942)
[].unshift(a, b)       0.280000   0.010000   0.290000 (  0.276724)

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