Why is Python so slow?

Eric Lee Green eric at estinc.com
Fri Jun 9 11:39:30 EDT 2000


William Dandreta wrote:
> I am just learning Python and I wrote a little program to modify a text file
> with 120,000 lines. It took 1hr and 30 mins. I wrote a C program that
> created the text file from a .dbf and it only took 2 mins.

> Why does it take Python 50 times longer than C?

This is actually a reasonable example of a problem set where you're using
Python when you should be using "C".

My guess is that you are attempting to do text parsing with raw Python (i.e.,
by walking down a string array) rather than using the built-in "C" tools to do
the same (string.split, string.replace, the whole regexp library, etc.). 

All that aside, there do exist problem domains where Python is clearly
significantly slower than a "C" implementation. At the Twofish for Python site
( http://twofish-py.sourceforge.net ) you will also find a "C" utility called
'aescrypt'. Despite the fact that Twofish for Python uses the exact same
underlying "C" module to do its encryptions, it takes 13 seconds to encrypt a
test file with aescrypt and over a minute to encrypt the same file with
encrypt.py (I think that's the name of the utility) -- or roughly 8 times
longer. It appears that most of that time is being spent allocating and
de-allocating strings (aescrypt uses a static encryption buffer). I wrote
aescrypt using the Python code as my guide, so I doubt that there's any other
major difference between the two programs that could account for the
difference in execution time. I guess the final reality is that yes, Virginia,
there do exist applications where Python is not appropriate -- such as
encrypting massive amounts of data. Thankfully I only intend to encrypt
passwords using Twofish for Python, rather than massive amounts of data.
(Note: If you are a Python programmer looking for a job, drop me some EMAIL --
especially if you've done GUI work in the past, you're hired :-). 

-- 
Eric Lee Green                         eric at estinc.com
Software Engineer                      Visit our Web page:
Enhanced Software Technologies, Inc.   http://www.estinc.com/
(602) 470-1115 voice                   (602) 470-1116 fax



More information about the Python-list mailing list