A story about Python... sort of

Ben Finney bignose-hates-spam at and-zip-does-too.com.au
Mon Jul 7 04:27:39 CEST 2003


On Mon, 07 Jul 2003 02:02:06 GMT, Russell Reagan wrote:
> Anyway, I know of one chess program written in python, and it is
> dreadfully slow compared to *any* program written in C/C++ (that I've
> seen anyway).

Have you looked at the code for it?  Have you profiled it to see where
its bottlenecks are?  It's often the case that a program is low because
of poor design, or simply choosing a slow algorithm.

Until profiling, of course, you can't know which algorithms are too slow
for the program.

> The things that make a chess program fast aren't really python's
> strengths, but hopefully I'm being pessimistic due to my lack of
> python/c knowledge.

My suggestion for those who want to write programs that do something
quickly:

  - Write a program that does the job at all, paying attention to
    simplicity and readability and *no* attention to optimisation.

  - Debug the program so it does everything it's supposed to do, albeit
    slowly.

  - Once the program is correct, and not before, profile it to see where
    it's slow.

  - Pick the biggest bottleneck and make it faster, by one or more of:

      - Choose a faster algorithm, perhaps sacrificing readability or
        simplicity
      - Re-implement in C

  - Iterate the previous step until the program is fast enough or you
    run out of (time|money).

You'll have a program that is quite readable, except in the places where
it needs to be fast.  In my experience, those places are surprisingly
few, and are surprisingly different to where you expected them to be.

> import chess
> chess.run_program_in_c
> print "Thanks for playing! Bye!"

It may well be that all the "real" chess-thinking stuff may be too slow
in Python; you might end up with the move-evaluation routine in C, for
example.

But discover that by profiling a Python implementation first!  If it
turns out to be too slow, at least you've debugged a working algorithm,
and can treat it as pseudocode for the port to C.  If it turns out that
the bottlenecks are somewhere else entirely, you've saved yourself a
huge misguided optimisation effort.

-- 
 \      "My roommate got a pet elephant. Then it got lost. It's in the |
  `\                           apartment somewhere."  -- Steven Wright |
_o__)                                                                  |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B




More information about the Python-list mailing list