Skip to content

strings: Count is O(n*m) #4600

@robpike

Description

@robpike
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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions