Created
February 14, 2011 03:42
-
-
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
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 '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 |
Author
ferrous26
commented
Feb 14, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment