Why is Python so slow?

Mike Fletcher mfletch at tpresence.com
Fri Jun 9 02:02:07 EDT 2000


1) You're using an _ancient_ version of Python... 5 years old?  Something
like that.  There's been at least 1 factor-of-two increase in speed since
then.
2) You might possibly be using poor python idioms.  For instance, you may be
using file.readlines() or the like, which, on a 486 (likely with limited
memory) would wind up taking forever to read the file into memory, then
would thrash as it processed the file, and finally would thrash on reading.
3) Low-powered machines often require low-level tools.
4) Python will seldom be as fast as C save where you are making heavy use of
extensions (such as numeric python)

On a PIII 500 under (stackless) Python 1.5.2, the following gives these
results...

p:\>speedy
Data file creation: 16.6439999342 seconds
Data file processing (simple): 20.239000082 seconds

Creation generates 360,000 random numbers and writes them to disk.
Processing consists of reading in each line, stripping off the white space,
splitting into fields, converting to floats, rounding to 0 decimal places,
then writing back to disk.
Total memory usage is about 2.5MB on my machine.

By this (very rough) timing, it seems the 486 is about 1/300th the speed of
the PIII?  I don't recall them being that slow, but I don't have any lying
around on which to test.  Factor in 1.5.2 vs 1.2 and it'd still be 1:150 .
I'm guessing there's something else causing a slowdown, or something
significantly different about your processing.

Enjoy yourself,
Mike

8<_________ speedy.py ___________
import string, random

def createDataFile( file = 'z:\\temp\\test.dat' ):
	file = open( file, 'w')
	for line in xrange( 120000 ):
		file.write( '%s %s %s\n'% (random.random(),
random.random(),random.random()) )
	file.close()
def processDataFile( file = 'z:\\temp\\test.dat' ):
	outfile = open( file+'.out', 'w')
	file = open( file)
	data = file.readlines( 16000 )
	while data:
		for line in data:
			line = map( round, map( float, string.split(
string.strip(line) )))
			if line:
				outfile.write( '%s %s %s\n'% tuple(line) )
		data = file.readlines( 16000 )

if __name__ == '__main__':
	import time
	t = time.time()
	createDataFile()
	print 'Data file creation: %s seconds'% (time.time()-t)
	t = time.time()
	processDataFile()
	print 'Data file processing (simple): %s seconds'% (time.time()-t)


-----Original Message-----
From: William Dandreta [mailto:wjdandreta at worldnet.att.net]
Sent: Friday, June 09, 2000 1:19 AM
To: python-list at python.org
Subject: Why is Python so slow?


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.

Both programs ran on the same machine (486-133mhz) under Concurrent DOS (a
multi-user mutitasking OS).

The Python version was 1.2.

Why does it take Python 50 times longer than C?

Bill Dandreta


-- 
http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list