Florian Gross wrote:
> 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.)
> 

The unshift operation is executed 49 times on a 7x7 Board. You find those 
unshift statements only within  if-blocks. These if-blocks are executed, if the 
search was successfull. Thus the unshift operation ist not the thing, that makes 
the algorithm slow.

the recursion is not that easy to understand. If you don't believe me, simply 
increase another counter as soon as one of  the if-blocks are being executed.

> 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