-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Closed
Labels
Milestone
Description
The simple algorithm used in strings.Count and bytes.Count is n^2 when linear algorithms are known, although they require precomputation and allocation. It may be worth looking at the parameters in the call to decide when a smarter algorithm is worthwhile. A simple non-allocating heuristic would be to check not only the first byte before using == but also the last byte. Another option would be to check the first and last bytes of the strings in the == operator before doing the O(n) scan, at least when n is large. Such tricks will not eliminate n^2 behavior but can make it less likely to arise. (Since in Count the lengths always match, the usual efficient cut-off of checking the lengths does nothing.) Noticed in https://plus.google.com/115609618223925128756/posts/5EG3QHA1ugH
Reactions are currently unavailable