Created
February 6, 2011 06:35
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MacRuby 0.9-nightly