10
votes

I was wondering if the 'find' method on strings was implemented with a linear search, or if python did something more sophisticated. The Python documentation doesn't discuss implementation details, so http://docs.python.org/library/stdtypes.html is of no help. Could someone please point me to the relevant source code?

3

3 Answers

22
votes

The comment on the implementation has the following to say:

fast search/count implementation, based on a mix between boyer-moore and horspool, with a few more bells and whistles on the top.

for some more background, see: http://effbot.org/zone/stringlib.htm

—https://github.com/python/cpython/blob/master/Objects/stringlib/fastsearch.h#L5

4
votes

You should be able to find it in Objects/stringlib/find.h, although the real code is in fastsearch.h.

1
votes

Looks like the algorithm used originates from Boyer-Moore-Horspool algorithm