optimization question
Andrew Koenig
ark at research.att.com
Sun Aug 11 19:29:57 CEST 2002
Peter> Anyway, you are optimizing before you've profiled and determined
Peter> you have a problem.
Correct.
The application I'm thinking of will do lots of comparisons of the form
s[i:j] == t
where s is very large (many megabytes), i and j are arbitrary (and might
be very far apart), and len(t) will almost always be small. I do not want
to create a lot of large substrings, only to find out that none of them
can possibly be equal to t because they're too big. And if I don't do
something about it in advance, it will be a real nuisance to find all of
the places where the code needs to change.
So, for example, in this case, I believe it is worth writing a function
that might look like this:
def eqsub(s, i, j, t):
return s[i:j] == t
and call this function everywhere instead of doing direct comparisons.
Then the optimization becomes trivial:
def eqsub(s, i, j, t):
return (len(t) == j-i) and s[i:j] == t
which avoids building the substrings unless necessary.
--
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark
More information about the Python-list
mailing list