From: Florian Gross Date: 2004-08-28T01:20:37+09:00 Subject: Re: performance comparison Boris Glawe wrote: > Hi, Moin! Let me point out two small optimizations. Maybe they will accumulate. > def find_trip (current_field) > > @recursion_count += 1 > > current_field.visited = true > @num_visited_fields += 1 > > if @num_visited_fields >= @num_fields > trip.unshift(current_field) @trip.unshift(current_field) > return > end > > for neighbour in current_field.neighbours > > next if neighbour.visited > > find_trip(neighbour) > if @num_visited_fields >= @num_fields > trip.unshift(current_field) @trip.unshift(current_field) > return > end > end > > current_field.visited = false > @num_visited_fields -= 1 > end Another thing you might want to note is that Array#unshift is quite a lot slower than Array#push (100000 iterations of #unshift take ~25 seconds and 100000 iterations of #push only ~0.07 seconds.) Output however only happens once so you could get away with just iterating over the Array in reverse there. (via #reverse_each) Regards, Florian Gross