(Unfortunately I have learned this the hard way)

An example slow input for this pattern is a bunch of spaces followed by a non-whitespace character. It tries starting at each space, and from each starting point it has to keep checking characters until it runs into the non-space character (at which point it realizes the pattern doesn't match). So it looks at about n characters, then about n - 1, then n - 2, etc, which adds up to O(n^2)

A short Python example

>>> import timeit
>>> timeit.timeit("re.findall(r'\s+$', 'a' + ' ' * 100)", setup="import re")
0.82954529998824
>>> timeit.timeit("re.findall(r'\s+$', ' ' * 100 + 'a')", setup="import re")
58.42058260005433

(but this is not in any way specific to Python, the actual problem I had was in Java)


You must log in to comment.

in reply to @amypercent's post: