Speed problems with Python vs. Perl

Fredrik Lundh fredrik at pythonware.com
Wed Mar 28 10:37:33 EST 2001


Georg Umgiesser wrote:

> ----------------------------------------------------------
>
> #!/usr/bin/python
>
> import sys
> import re
>
> whitespace = re.compile("\s+")
>
> def main():
>
>     icount = 0
>     for line in sys.stdin.readlines():
>         icount = icount + 1
>         f = whitespace.split(line)
>     print "Total lines read: " + `icount`
>
> if __name__ == '__main__':
>     main()
>
> --------------------------------------------------------

assuming you're using Python 2.1, the following version is
about 16 times faster on my box:

import sys

def main():

    icount = 0
    for line in sys.stdin.xreadlines():
        icount += 1
        f = line.split()
    print "Total lines read", icount

if __name__ == '__main__':
    main()

for a backwards-compatible version of xreadlines, use
the double-loop pattern from:

http://effbot.org/guides/readline-performance.htm

Cheers /F





More information about the Python-list mailing list