Can I beat perl at grep-like processing speed?

js ebgssth at gmail.com
Fri Dec 29 10:00:35 EST 2006


Just my curiosity.
Can python beats perl at speed of grep-like processing?


$ wget http://www.gutenberg.org/files/7999/7999-h.zip
$ unzip 7999-h.zip
$ cd 7999-h
$ cat *.htm > bigfile
$ du -h bigfile
du -h bigfile
8.2M	bigfile

---------- grep.pl ----------
#!/usr/local/bin/perl
open(F, 'bigfile') or die;

while(<F>) {
  s/[\n\r]+$//;
  print "$_\n" if m/destroy/oi;
}
---------- END ----------
---------- grep.py ----------
#!/usr/bin/env python
import re
r = re.compile(r'destroy', re.IGNORECASE)

for s in file('bigfile'):
  if r.search(s): print s.rstrip("\r\n")
---------- END ----------

$ time perl grep.pl  > pl.out; time python grep.py > py.out
real	0m0.168s
user	0m0.149s
sys	0m0.015s

real	0m0.450s
user	0m0.374s
sys	0m0.068s
# I used python2.5 and perl 5.8.6



More information about the Python-list mailing list