python vs. grep
Arnaud Delobelle
arnodel at googlemail.com
Tue May 6 16:35:24 EDT 2008
Anton Slesarev <slesarev.anton at gmail.com> writes:
> f = open("bigfile",'r')
>
> flines = (line for line in f if pat.search(line))
> c=0
> for x in flines:
> c+=1
> print c
It would be simpler (and probably faster) not to use a generator expression:
search = re.compile('sometext').search
c = 0
for line in open('bigfile'):
if search(line):
c += 1
Perhaps faster (because the number of name lookups is reduced), using
itertools.ifilter:
from itertools import ifilter
c = 0
for line in ifilter(search, 'bigfile'):
c += 1
If 'sometext' is just text (no regexp wildcards) then even simpler:
...
for line in ...:
if 'sometext' in line:
c += 1
I don't believe you'll easily beat grep + wc using Python though.
Perhaps faster?
sum(bool(search(line)) for line in open('bigfile'))
sum(1 for line in ifilter(search, open('bigfile')))
...etc...
All this is untested!
--
Arnaud
More information about the Python-list
mailing list