Py 3 slower than Py 2. Towers of Hanoi implementation

Paul Moore p.f.moore at gmail.com
Fri Jun 26 11:01:24 EDT 2009


2009/6/26 Udyant Wig <udyantw at gmail.com>:
> I implemented this -> http://www.apl.jhu.edu/~hall/lisp/Hanoi.lisp 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
>
>        else:
>                hanoi ((num_discs - 1), start, (remaining_peg (start, end)))
>                hanoi (1, start, end)
>                hanoi ((num_discs - 1), (remaining_peg (start, end)), end)
>
>
> hanoi(20,2,3)
>
> 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?

I/O in Python 3.0 is known to be slow, because the bulk of the
implementation is in Python (rather than C). Python 3.1 addresses
this. Here are some tests of 2.6.1 vs 3.1rc2. The first tests have the
print commented out, the second are with the print, redirected to the
null device. The tests are on a pretty slow Windows XP SP2 laptop.

>timer & \Apps\Python26\python.exe hanoi.py & timer
Timer 1 on: 15:54:53
Timer 1 off: 15:54:57  Elapsed: 0:00:03.88

>timer & \Apps\Python31\python.exe hanoi.py & timer
Timer 1 on: 15:57:05
Timer 1 off: 15:57:09  Elapsed: 0:00:03.67

>timer & (\Apps\Python26\python.exe hanoi.py >nul) & timer
Timer 1 on: 15:57:44
Timer 1 off: 15:58:14  Elapsed: 0:00:29.91

>timer & (\Apps\Python31\python.exe hanoi.py >nul) & timer
Timer 1 on: 15:58:38
Timer 1 off: 15:59:09  Elapsed: 0:00:30.63

Nothing much in it.

Paul.



More information about the Python-list mailing list