You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Optimize str.count method and do analysis against cpython #2355
Current algorithm of str.count basically implements naive algorithm, having all time complexities as O(n*m) where n stands for length of string and m stands for length of substring to be found, this implementation is most likely even slower than cpython once, since, it has a very very lower average time complexity.
Implement better algo and put analysis of it to make sure implementations are comparatively faster than cpython.
/* note: fastsearch may access s[n], which isn't a problem when using
Python's ordinary string types, but may cause problems if you're
using this code in other contexts. also, the count mode returns -1
if there cannot possibly be a match in the target string, and 0 if
it has actually checked for matches, but didn't find any. callers
beware! */
/* If the strings are long enough, use Crochemore and Perrin's Two-Way
algorithm, which has worst-case O(n) runtime and best-case O(n/k).
Also compute a table of shifts to achieve O(n/k) in more cases,
and often (data dependent) deduce larger shifts than pure C&P can
deduce. See stringlib_find_two_way_notes.txt in this folder for a
detailed explanation. */
Activity