Python IO performance?

Andreas Jung lists at andreas-jung.com
Sat May 31 04:56:14 EDT 2003


try:

data =open(filename).read()

or
lines = open(filename).readlines()

-aj

--On Samstag, 31. Mai 2003 12:56 Uhr +0530 Ganesan R 
<rganesan at myrealbox.com> wrote:

>
> 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
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list









More information about the Python-list mailing list