Skip to content

Instantly share code, notes, and snippets.

@spenserfiller
Created December 13, 2014 07:28
Show Gist options
  • Select an option

  • Save spenserfiller/a9bb02aeaebaaba912c7 to your computer and use it in GitHub Desktop.

Select an option

Save spenserfiller/a9bb02aeaebaaba912c7 to your computer and use it in GitHub Desktop.
Ruby Snail Benchmark
require 'benchmark'
def snail2(array)
array.empty? ? [] : array[0] + snail2(array[1..-1].transpose.reverse)
end
def snail_spenser(array)
horiz = 0;
vert = 0;
result = [];
tracker = 0;
length = array.length;
direction = "right"
(1..(array.length*array.length)).each do
if direction == "right"
result.push(array[vert][horiz])
if horiz < length-1-tracker
horiz += 1
else
direction = "down"
vert += 1
end
elsif direction == "down"
result.push(array[vert][horiz])
if vert < length-1-tracker
vert += 1
else
direction = "left"
horiz -= 1
end
elsif direction == "left"
result.push(array[vert][horiz])
if horiz > 0+tracker
horiz -= 1
else
direction = "up"
vert -= 1
tracker += 1
end
elsif direction == "up"
result.push(array[vert][horiz])
if vert > 0+tracker
vert -= 1
else
direction = "right"
horiz += 1
end
end
end
result;
end
def snail1(array)
l=array.size
r=[]
i=0
r << array[i]
id=1
jd=-1
lmt = j = l-1
((l-1)*2).times{
lmt.times{
i+=(1*id)
r << array[i][j]
}
lmt.times{
j+=(1*jd)
r << array[i][j]
}
id*=-1
jd*=-1
lmt-=1
}
r.flatten
end
def snail_julia(lines)
arr = []
snailit(lines, arr)
arr.flatten
end
def snailit(lines, arr)
if lines.length > 0
arr << lines[0]
#do first line
if lines.length > 2
middles = lines[1..-2]
middles.each{|line| #do right side of middles
arr << line[-1]
line.pop
}
end
if lines.length > 1
arr << lines[-1].reverse #do last line
end
if lines.length > 2
revm = middles.reverse
revm.each{|line| #do left side of middles
arr << line[0]
line.shift
}
end
lines.pop
lines.shift
snailit(lines, arr) #recursion
end
arr
end
def snail_jeff(arr)
ans = []
until arr.empty?
ans2 = []
ans.push(arr[0])
arr = arr[1..-1]
arr.each {|x|ans.push(x.pop)}
ans.push(arr.pop.reverse)
arr.each_index{|x| ans2.push(arr[x].first); arr[x] = arr[x][1..-1]}
until ans2.empty?
ans.push(ans2.pop)
end
end
return ans.flatten
end
def snail_jeff2(arr)
ans = []
until arr.empty?
ans2 = []
ans.push(arr[0])
arr = arr[1..-1]
arr.each {|x|ans.push(x.pop)}
ans2.push(arr.pop)
until ans2.empty?
ans.push(ans2.pop)
end
arr.each_index{|x| ans2.push(arr[x].first); arr[x] = arr[x][1..-1]}
until ans2.empty?
ans.push(ans2.pop)
end
end
return ans.flatten
end
def slow_snail array
array.empty? ? [] : array.first + slow_snail(array[1..-1].transpose.reverse)
end
Benchmark.bm do |x|
array = [[1,2,3,4,5,6,7],
[22,27,24,25,26,27,8],
[21,36,33,34,25,28,9],
[20,35,36,35,26,29,10],
[19,34,33,32,31,30,11],
[18,17,16,15,14,13,12],
[18,17,16,15,14,13,12]]
x.report('Spenser'){ 100000.times do ; snail_spenser(array);
end }
x.report('x2') { 100000.times do ; snail2(array);
end }
x.report('Nick') { 100000.times do ; slow_snail(array);
end }
x.report('Julia') { 100000.times do ; snail_julia(array);
end }
x.report('x1') { 100000.times do ; snail1(array);
end }
x.report('Jeff') { 100000.times do ; snail_jeff(array);
end }
x.report('jeff 2') { 100000.times do ; snail_jeff2(array);
end }
x.report('Nick') { 100000.times do ; slow_snail(array);
end }
end
# x2 2.080000 0.000000 2.080000 ( 2.084230)
# Nick 2.070000 0.000000 2.070000 ( 2.068219)
# Julia 0.080000 0.000000 0.080000 ( 0.080981)
# x1 0.090000 0.000000 0.090000 ( 0.089529)
# Jeff 0.070000 0.000000 0.070000 ( 0.066896)
# jeff 2 0.060000 0.000000 0.060000 ( 0.066855)
# Spenser 0.050000 0.000000 0.050000 ( 0.042322)
# Spenser 5.930000 0.010000 5.940000 ( 5.942810)
# x2 6.140000 0.020000 6.160000 ( 6.153332)
# Nick 6.320000 0.010000 6.330000 ( 6.337475)
# Julia 0.230000 0.000000 0.230000 ( 0.222817)
# x1 0.250000 0.000000 0.250000 ( 0.261213)
# Jeff 0.200000 0.000000 0.200000 ( 0.195534)
# jeff 2 0.200000 0.000000 0.200000 ( 0.197776)
# Nick 0.030000 0.000000 0.030000 ( 0.034668)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment