Can I beat perl at grep-like processing speed?

Nick Craig-Wood nick at craig-wood.com
Sat Dec 30 01:32:53 EST 2006


js <ebgssth at gmail.com> wrote:
>  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
> 
>  #!/usr/local/bin/perl
>  open(F, 'bigfile') or die;
> 
>  while(<F>) {
>    s/[\n\r]+$//;
>    print "$_\n" if m/destroy/oi;
>  }
>  #!/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")
> 
>  $ 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

Playing for the other side temporarily, this is nearly twice as fast...

  $ time perl -lne 'print if m/destroy/oi' bigfile >pl.out
  real    0m0.133s
  user    0m0.120s
  sys     0m0.012s

vs

  $ time ./z.pl >pl.out.orig
  real    0m0.223s
  user    0m0.208s
  sys     0m0.016s

Which gives the same output modulo a few \r

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list