Skip to content

Instantly share code, notes, and snippets.

@ferrous26
Created February 6, 2011 06:35
Show Gist options
  • Select an option

  • Save ferrous26/813190 to your computer and use it in GitHub Desktop.

Select an option

Save ferrous26/813190 to your computer and use it in GitHub Desktop.
Benchmarking shifting through an array versus calculating an offset when you need i to be i bet get at position i-1
require 'benchmark'
array_iterate = (1..10_000_000).to_a
array_shift = array_iterate.dup
Benchmark.bmbm do |test|
# this assumes you can destroy the given array
test.report 'using shift' do
for i in array_shift
array_shift.shift
end
end
test.report 'using an offset' do
for i in array_iterate
array_iterate[i-1]
end
end
end
@ferrous26
Copy link
Copy Markdown
Author

MacRuby 0.9-nightly

Rehearsal ---------------------------------------------------
using shift       0.560000   0.000000   0.560000 (  0.561027)
using an offset   0.630000   0.010000   0.640000 (  0.654486)
------------------------------------------ total: 1.200000sec

                      user     system      total        real
using shift       0.280000   0.000000   0.280000 (  0.281825)
using an offset   0.630000   0.000000   0.630000 (  0.661343)

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