Py 3 slower than Py 2. Towers of Hanoi implementation

U

Udyant Wig

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?
 
S

Stefan Behnel

Udyant said:
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?

Have you tried Python 3.1? Have you made sure that both Python interpreters
were build with the same compiler arguments and that none of them is a
debug build?

Stefan
 
U

Udyant Wig

Have you tried Python 3.1? Have you made sure that both Python interpreters
were build with the same compiler arguments and that none of them is a
debug build?

Stefan

Yes. Both were built with the same compiler parameters.
No. None of them is a debug build.
I'm currently getting Python 3.1
 
P

Paul Moore

2009/6/26 Udyant Wig said:
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.
 
U

Udyant Wig

2009/6/26 Udyant Wig <[email protected]>:










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 1 on: 15:54:53
Timer 1 off: 15:54:57  Elapsed: 0:00:03.88


Timer 1 on: 15:57:05
Timer 1 off: 15:57:09  Elapsed: 0:00:03.67


Timer 1 on: 15:57:44
Timer 1 off: 15:58:14  Elapsed: 0:00:29.91


Timer 1 on: 15:58:38
Timer 1 off: 15:59:09  Elapsed: 0:00:30.63

Nothing much in it.

Paul.

Thank you for clearing that up.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top