I have a huge amount (GB) of text to process, sentence by sentence.
In each sentence I have a costly operation to perform on numbers, so I check that this sentence contains at least one digit.
I have done this check using different means and measured those solutions using timeit
.
s = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' # example
any(c.isdigit() for c in s)
3.61 µsre.search('\d', s)
402 nsd = re.compile('\d')
d.search(s)
126 ns'0' in s or '1' in s or '2' in s or '3' in s or '4' in s or '5' in s or '6' in s or '7' in s or '8' in s or '9' in s
60ns
The last way is the fastest one, but it is ugly and probably 10x slower than possible.
Of course I could rewrite this in cython, but it seems overkill.
Is there a better pure python solution? In particular, I wonder why you can use str.startswith()
and str.endswith()
with a tuple argument, but it does not seem to be possible with in
operator.