
Untitled
By: a guest on
Apr 15th, 2012 | syntax:
None | size: 0.52 KB | hits: 7 | expires: Never
def recur_length list
return 0 if list.empty?
1 + recur_length(list.drop 1 )
end
nums = (1..1000).to_a
start = Time.now
recur_length nums
fin = Time.now
puts "list size of 1000..."
puts "recursive: #{fin - start} seconds"
def iter_length list
size = 0
list.each {|i| size += 1}
size
end
nums = (1..1000).to_a
start = Time.now
iter_length nums
fin = Time.now
puts "non-recursive: #{fin - start} seconds"
###### output #######
# list size of 1000...
# recursive: 0.000437 seconds
# non-recursive: 0.000128 seconds