Can I beat perl at grep-like processing speed?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Tue Jan 2 17:01:28 CET 2007


js a écrit :
> Just my curiosity.
> Can python beats perl at speed of grep-like processing?

Probably not.

> 
> $ 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 ----------

Please notice that you're also benchmarking IO here - and perl seems to 
use a custom, highly optimized IO lib, that is much much faster than the 
system's one. I once made a Q&D cat-like comparison of perl, Python and 
C on my gentoo-linux box, and the perl version was insanely faster than 
the C one.

Now the real question is IMHO: is the Python version fast enough ?

My 2 cents..



More information about the Python-list mailing list