Last active
December 24, 2015 01:29
-
-
Save TalkativeTree/6724065 to your computer and use it in GitHub Desktop.
This file contains 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' | |
require 'bigdecimal/math' | |
number = 0 | |
array = Array.new(15){ Array.new(15, number += 1) } | |
def spiral(array) | |
spiral = [] | |
spiral << array.delete_at(0) | |
return spiral.flatten if array.empty? | |
spiral += spiral(array.transpose.reverse) | |
end | |
def spiral_refactor(array) | |
spiral = [] | |
spiral << array.delete_at(0) | |
spiral += spiral(array.transpose.reverse) unless array.empty? | |
spiral.flatten | |
end | |
def spiral_splat(array) # not mine: https://gist.github.com/jfarmer/d0f37717f6e7f6cebf72 | |
return [] if array.empty? | |
first, *rest = *array | |
return first + spiral(rest.transpose.reverse) | |
end | |
p spiral(array) | |
puts Benchmark.measure { 1000.times { spiral(array) } } | |
# 0.000000 0.000000 0.000000 ( 0.003572) | |
# 0.000000 0.000000 0.000000 ( 0.003431) | |
# 0.000000 0.000000 0.000000 ( 0.003619) | |
puts Benchmark.measure { 1000.times { spiral_refactor(array) } } | |
# 0.000000 0.000000 0.000000 ( 0.001334) | |
# 0.000000 0.000000 0.000000 ( 0.001342) | |
# 0.000000 0.000000 0.000000 ( 0.001271) | |
puts Benchmark.measure { 1000.times { spiral_splat(array) } } | |
# 0.000000 0.000000 0.000000 ( 0.000334) | |
# 0.000000 0.000000 0.000000 ( 0.000328) | |
# 0.000000 0.000000 0.000000 ( 0.000342) | |
#This is the resulting array of integers split by line. | |
#[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
#2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, | |
#15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | |
#14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, | |
#2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
#3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, | |
#14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, | |
#13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, | |
#3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, | |
#4, 5, 6, 7, 8, 9, 10, 11, 12, 13, | |
#13, 13, 13, 13, 13, 13, 13, 13, 13, 13, | |
#12, 11, 10, 9, 8, 7, 6, 5, 4, | |
#4, 4, 4, 4, 4, 4, 4, 4, 4, | |
#5, 6, 7, 8, 9, 10, 11, 12, | |
#12, 12, 12, 12, 12, 12, 12, 12, | |
#11, 10, 9, 8, 7, 6, 5, | |
#5, 5, 5, 5, 5, 5, 5, | |
#6, 7, 8, 9, 10, 11, | |
#11, 11, 11, 11, 11, 11, | |
#10, 9, 8, 7, 6, | |
#6, 6, 6, 6, 6, | |
#7, 8, 9, 10, | |
#10, 10, 10, 10, | |
#9, 8, 7, | |
#7, 7, 7, | |
#8, 9, | |
#9, 9, | |
#8, 8] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment