sobering observation, python vs. perl

Charles T. Smith cts.private.yahoo at gmail.com
Thu Mar 17 11:29:47 EDT 2016


I've really learned to love working with python, but it's too soon
to pack perl away.  I was amazed at how long a simple file search took
so I ran some statistics:

    $ time python find-rel.py
    ./find-relreq *.out | sort -u
    TestCase_F_00_P
    TestCase_F_00_S
    TestCase_F_01_S
    TestCase_F_02_M

    real    1m4.581s
    user    1m4.412s
    sys     0m0.140s


    $ time python find-rel.py
    # modified to use precompiled REs:
    TestCase_F_00_P
    TestCase_F_00_S
    TestCase_F_01_S
    TestCase_F_02_M

    real    0m29.337s
    user    0m29.174s
    sys     0m0.100s


    $ time perl find-rel.pl
    find-relreq.pl *.out | sort -u
    TestCase_F_00_P
    TestCase_F_00_S
    TestCase_F_01_S
    TestCase_F_02_M

    real    0m5.009s
    user    0m4.932s
    sys     0m0.072s

Here's the programs:

#!/usr/bin/env python
# vim: tw=0
import sys
import re

isready = re.compile ("(.*) is ready")
relreq = re.compile (".*release_req")
for fn in sys.argv[1:]:                                 # logfile name
    tn = None
    with open (fn) as fd:
        for line in fd:
            #match = re.match ("(.*) is ready", line)
            match = isready.match (line)
            if match:
                tn = match.group(1)
            #match = re.match (".*release_req", line)
            match = relreq.match (line)
            if match:
                #print "%s: %s" % (tn, line),
                print tn

vs.

while (<>) {
    if (/(.*) is ready/) {
        $tn = $1;
    }
    elsif (/release_req/) {
        print "$tn\n";
    }
}

Look at those numbers:
1 minute for python without precompiled REs
1/2 minute with precompiled REs
5 seconds with perl.



More information about the Python-list mailing list