Created
January 26, 2012 15:22
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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