Skip to content

Instantly share code, notes, and snippets.

@ferrous26
Created February 14, 2011 03:42
Show Gist options
  • Select an option

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

Select an option

Save ferrous26/825452 to your computer and use it in GitHub Desktop.
Testing if it is faster to split an array by dup/shift/splat or index/index/splat
require 'rubygems'
gem 'minitest'
require 'minitest/autorun'
require 'minitest/benchmark'
class Bench < MiniTest::Unit::TestCase
def self.test_order
:alpha
end
def self.bench_range
bench_exp 1_000, 1_000_000
end
TABLE = {
first: [:second, :third],
one: [:two, :three, :four],
}
def bench_lookup_dup_shift_splat
assert_performance_linear 0.99 do |n|
entry = TABLE[:first]
n.times {
thing = entry.dup
thing.shift
test = *thing
}
entry = TABLE[:one]
n.times {
thing = entry.dup
thing.shift
test = *thing
}
end
end
def bench_lookup_index_splat
assert_performance_linear 0.99 do |n|
entry = TABLE[:first]
n.times {
entry[0]
test = *entry[1..-1]
}
entry = TABLE[:one]
n.times {
entry[0]
test = *entry[1..-1]
}
end
end
end
@ferrous26
Copy link
Copy Markdown
Author

 Bench                               1000       10000   100000  1000000
 bench_lookup_dup_shift_splat      0.001170  0.011303    0.133173    1.356868
 bench_lookup_index_splat    0.000525    0.005105    0.077832    0.843958

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