Skip to content

Instantly share code, notes, and snippets.

@takluyver
Created May 19, 2012 11:28
Show Gist options
  • Save takluyver/2730521 to your computer and use it in GitHub Desktop.
Save takluyver/2730521 to your computer and use it in GitHub Desktop.
Timing string tests
# Python 3
In [1]: from string import ascii_letters
In [2]: s = ascii_letters*1000 + 'ש' + ascii_letters*1000
In [3]: %timeit r1 = any("\u0590" <= c <= "\u05EA" for c in s)
100 loops, best of 3: 6.26 ms per loop
In [4]: chars = {chr(x) for x in range(0x590, 0x5EA + 1)}
In [5]: %timeit r2 = any(c in chars for c in s) # Marcin's suggestion 1
100 loops, best of 3: 5.82 ms per loop
In [6]: import re
In [7]: hebrew_re = re.compile("[" + "".join(chars) + "]")
In [8]: %timeit r3 = hebrew_re.search(s) # Marcin's suggestion 2
1000 loops, best of 3: 402 us per loop
In [15]: %timeit any(c in s for c in chars) # Test for membership in the long string
100 loops, best of 3: 1.81 ms per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment