optimization question

Paul Rubin phr-n2002b at NOSPAMnightsong.com
Mon Aug 12 18:43:51 EDT 2002


Andrew Koenig <ark at research.att.com> writes:
> Right.  In the application I was thinking of, i and j are determined by
> one part of the program and t is determined in another part, and they
> don't talk to each other.  So I have three choices:
> 
>         1) Write s[i:j]==t and be aware that the comparison might be
>            expensive even if t is short;
> 
>         2) Write s[i:j]==t, secure in the knowledge that the implementation
>            will do the comparison without extracting the substring; or
> 
>         3) Write (len(t)==j-1 and s[i:j]==t), assuming, of course, that
>            i and j have the same sign.
> 
> I've learned that (2) is not an option, so now I have to decide between
> (1) and (3) -- a decision I'll make when I have to make it.

4) write an explicit loop comparing element by element.  This will have
the O(N) behavior that you want, but the constant factor out front will
be far worse than comparing the equality of reasonably sized slices.



More information about the Python-list mailing list