Python IO performance?

Ganesan R rganesan at myrealbox.com
Sat May 31 03:26:57 EDT 2003


Hi,

I apologize for bringing up this topic again. I am sure I am missing
something obvious. I am a relatively recent perl to python convert
and I always had a nagging feeling that processing text files with
python was slow. I set about experimenting. Here's my version of
cat in python

====
#!/usr/bin/python2.2

import fileinput

for line in fileinput.input():
    print line,
====	

Trying this on a standard Redhat 7.3 install with a nearly 3MB log file
I get

=====
$ time python2 ./mycat.py logrotate.status > /dev/null
 
real 0m0.716s
user 0m0.620s
sys 0m0.090s
======

It's consistently over 0.7 secs real time for multiple runs.

Here's my equivalent perl code:

====
#!/usr/bin/perl -w

while (<>) {
    print;
}

==== 

Timing this

=====
$ time perl ./mycat.pl logrotate.status > /dev/null

real 0m0.086s
user 0m0.080s
sys 0m0.000s
=====

Python is over 8 times slower! Is the problem with the fileinput
module or is I/O just slower with python?

Ganesan






More information about the Python-list mailing list