Can I beat perl at grep-like processing speed?
Christophe Cavalaria
chris.cavalaria at free.fr
Fri Dec 29 10:28:21 EST 2006
js 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
>
> ---------- 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
I'm thankful for the Python version or else, I'd never have guessed what
that code was supposed to do!
Try that :
---------- grep.py ----------
#!/usr/bin/env python
import re
def main():
search = re.compile(r'destroy', re.IGNORECASE).search
for s in file('bigfile'):
if search(s): print s.rstrip("\r\n")
main()
---------- END ----------
More information about the Python-list
mailing list