[Python-Dev] Re: string find(substring) vs. substring in string

Fredrik Lundh fredrik at pythonware.com
Wed Feb 16 22:23:03 CET 2005


Mike Brown wrote:
>> any special reason why "in" is faster if the substring is found, but
>> a lot slower if it's not in there?
>
> Just guessing here, but in general I would think that it would stop searching
> as soon as it found it, whereas until then, it keeps looking, which takes more
> time.

the point was that string.find does the same thing, but is much faster in
the "no match" case.

> But I would also hope that it would be smart enough to know that it
> doesn't need to look past the 2nd character in 'not the xyz' when it is
> searching for 'not there' (due to the lengths of the sequences).

note that the target string was "not the xyz"*100, so the search algorithm
surely has to look past the second character ;-)

(btw, the benchmark was taken from jim hugunin's ironpython talk, and
seems to be carefully designed to kill performance also for more advanced
algorithms -- including boyer-moore)

</F> 





More information about the Python-Dev mailing list