Skip to content

Instantly share code, notes, and snippets.

@umbrae
Created January 26, 2012 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umbrae/1683249 to your computer and use it in GitHub Desktop.
Save umbrae/1683249 to your computer and use it in GitHub Desktop.
Speed comparison of finding if a digit exists in a string, in python.
>>> cmd = """
... for c in 'xdtwkeltjwlkejt7wthwk89lk':
... if c.isdigit():
... break;
... """
>>>
>>> timeit.Timer(cmd).timeit()
2.1454498767852783
>>>
>>>
>>> setup = """
... import re
... """
>>>
>>> cmd = """
... re.search('\d', 'xdtwkeltjwlkejt7wthwk89lk')
... """
>>>
>>> timeit.Timer(cmd, setup=setup).timeit()
1.8365380764007568
>>>
>>> cmd = """
... next((c for c in 'xdtwkeltjwlkejt7wthwk89lk' if c.isdigit()), -1)
... """
>>>
>>> timeit.Timer(cmd).timeit()
3.0304851531982422
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment