noro <am******@gmail .comwrote:
>OK, am not sure why, but
fList=file('so mefile').read()
if fList.find('str ing') != -1:
print 'FOUND'
works much much faster.
it is strange since i thought 'for line in file('somefile' )' is
optemized and read pages to the memory,
Step back and think about what each is doing at a high level of
description: file.read reads the contents of the file into memory
in one go, end of story. file.[x]readlines reads (some or all of)
the contents of the file into memeory, does a linear searches on it
for end of line characters, and copies out the line(s) into some
new bits of memory. Line-by-line processing has a *lot* more work
to do (unless you're read()ing a really big file which is going to
make heavy demands on memory allocation) and it should be no
surprise that it's slower.
--
\S --
si***@chiark.gr eenend.org.uk --
http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump