Py 3 slower than Py 2. Towers of Hanoi implementation

Udyant Wig udyantw at
Fri Jun 26 10:07:51 EDT 2009

I implemented this -> in
both flavors of Python: 2.6.2 and 3.0.1 (CPython)

The code:
#!/usr/bin/env python
def remaining_peg (peg1, peg2):
	return (6 - peg1 - peg2)

def hanoi (num_discs, start, end):
	if (1 == num_discs):
		print "Top of peg {0} to peg {1}".format(start,end) # used print()
for Py 3.0.1

		hanoi ((num_discs - 1), start, (remaining_peg (start, end)))
		hanoi (1, start, end)
		hanoi ((num_discs - 1), (remaining_peg (start, end)), end)


The times:            real          usr          sys
Python 2.6.2       7.994s    3.336s   3.296s
Python 3.0.1       55.302s  38.024s 5.876s

What happened to Python?

More information about the Python-list mailing list