optimization question

Andrew Koenig ark at research.att.com
Mon Aug 12 10:05:37 EDT 2002


>> Yes, it looks like it would.  On the other hand, I might want
>> to generalize the technique to sequences other than strings...

Terry> def subseqmatch(seq, i, j, sub):
Terry>   if len(sub) != j-i: return False
Terry>   for item in sub:
Terry>     if item != seq[i]: return False
Terry>     i += 1
Terry>   return True

Yes, I can always do it longhand, as it were.

I'd like to take a step back for a moment.  I originally asked a
simple question: If I write an expression of the form s[i:j] == x, can
I count on the implementation optimizing it by avoiding a copy of
s[i:j], so that I can be assured of not having to think about finding
more efficient alternatives?  So far, in addition to answers to the
question I asked, I have gotten answers to a number of questions that
I did not ask, such as:

        Should I replace s[i:j] == x by something else?

        Is the mere presence of code of the form s[i:j] == x
        in my program evidence of bad design?

        Is it proper to ask about the algorithmic complexity
        of a programming-language implementation?

All of these may be interesting questions, but they aren't the one
I asked.  You're welcome to discuss them, of course, but please don't
assume that I asked them.

-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark



More information about the Python-list mailing list