code_junkie

Is it worth using Python's re.compile

Nov 13th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. h = re.compile('hello')
  2. h.match('hello world')
  3.  
  4. re.match('hello', 'hello world')
  5.  
  6. def match(pattern, string, flags=0):
  7. return _compile(pattern, flags).match(string)
  8.  
  9. def _compile(*key):
  10.  
  11. # Does cache check at top of function
  12. cachekey = (type(key[0]),) + key
  13. p = _cache.get(cachekey)
  14. if p is not None: return p
  15.  
  16. # ...
  17. # Does actual compilation on cache miss
  18. # ...
  19.  
  20. # Caches compiled regex
  21. if len(_cache) >= _MAXCACHE:
  22. _cache.clear()
  23. _cache[cachekey] = p
  24. return p
  25.  
  26. num = "..."
  27. # then, much later:
  28. m = re.match(num, input)
  29.  
  30. num = re.compile("...")
  31. # then, much later:
  32. m = num.match(input)
  33.  
  34. $ python -m timeit -s "import re" "re.match('hello', 'hello world')"
  35. 100000 loops, best of 3: 3.82 usec per loop
  36.  
  37. $ python -m timeit -s "import re; h=re.compile('hello')" "h.match('hello world')"
  38. 1000000 loops, best of 3: 1.26 usec per loop
  39.  
  40. foo_pat = re.compile('foo',re.I)
  41.  
  42. foo_pat.findall('some string FoO bar')
  43. >> ['FoO']
  44.  
  45. re.findall('(?i)foo','some string FoO bar')
  46. >> ['FoO']
  47.  
  48. import re
  49. import time
  50.  
  51. rgx = re.compile('(w+)s+[0-9_]?s+w*')
  52. str = "average 2 never"
  53. a = 0
  54.  
  55. t = time.time()
  56.  
  57. for i in xrange(1000000):
  58. if re.match('(w+)s+[0-9_]?s+w*', str):
  59. #~ if rgx.match(str):
  60. a += 1
  61.  
  62. print time.time() - t
  63.  
  64. import re
  65. import time
  66.  
  67. rgx = re.compile('(w+)s+[0-9_]?s+w*')
  68. str = "average 2 never"
  69. a = 0
  70.  
  71. t = time.time()
  72.  
  73. for i in xrange(1000000):
  74. if re.match('(w+)s+[0-9_]?s+w*', str):
  75. #~ if rgx.match(str):
  76. a += 1
  77.  
  78. print time.time() - t
  79.  
  80. import re
  81. import time
  82.  
  83. rgx = re.compile('(w+)s+[0-9_]?s+w*')
  84. str = "average 2 never"
  85. a = 0
  86.  
  87. t = time.time()
  88.  
  89. for i in xrange(1000000):
  90. if re.match('(w+)s+[0-9_]?s+w*', str):
  91. #~ if rgx.match(str):
  92. a += 1
  93.  
  94. print time.time() - t
  95.  
  96. import re
  97. import time
  98. import fpformat
  99. Regex1 = re.compile('^(a|b|c|d|e|f|g)+$')
  100. Regex2 = re.compile('^[a-g]+$')
  101. TimesToDo = 1000
  102. TestString = ""
  103. for i in range(1000):
  104. TestString += "abababdedfg"
  105. StartTime = time.time()
  106. for i in range(TimesToDo):
  107. Regex1.search(TestString)
  108. Seconds = time.time() - StartTime
  109. print "Alternation takes " + fpformat.fix(Seconds,3) + " seconds"
  110.  
  111. StartTime = time.time()
  112. for i in range(TimesToDo):
  113. Regex2.search(TestString)
  114. Seconds = time.time() - StartTime
  115. print "Character Class takes " + fpformat.fix(Seconds,3) + " seconds"
  116.  
  117. Alternation takes 2.299 seconds
  118. Character Class takes 0.107 seconds
  119.  
  120. import re
  121. import time
  122. import fpformat
  123.  
  124. TimesToDo = 1000
  125. TestString = ""
  126. for i in range(1000):
  127. TestString += "abababdedfg"
  128. StartTime = time.time()
  129. for i in range(TimesToDo):
  130. re.search('^(a|b|c|d|e|f|g)+$',TestString)
  131. Seconds = time.time() - StartTime
  132. print "Alternation takes " + fpformat.fix(Seconds,3) + " seconds"
  133.  
  134. StartTime = time.time()
  135. for i in range(TimesToDo):
  136. re.search('^[a-g]+$',TestString)
  137. Seconds = time.time() - StartTime
  138. print "Character Class takes " + fpformat.fix(Seconds,3) + " seconds"
  139.  
  140. Alternation takes 2.508 seconds
  141. Character Class takes 0.109 seconds
  142.  
  143. """ Re.py: Re.match = re.match + cache
  144. efficiency: re.py does this already (but what's _MAXCACHE ?)
  145. readability, inline / separate: matter of taste
  146. """
  147.  
  148. import re
  149.  
  150. cache = {}
  151. _re_type = type( re.compile( "" ))
  152.  
  153. def match( pattern, str, *opt ):
  154. """ Re.match = re.match + cache re.compile( pattern )
  155. """
  156. if type(pattern) == _re_type:
  157. cpat = pattern
  158. elif pattern in cache:
  159. cpat = cache[pattern]
  160. else:
  161. cpat = cache[pattern] = re.compile( pattern, *opt )
  162. return cpat.match( str )
  163.  
  164. # def search ...
  165.  
  166. from re import compile as _Re
  167.  
  168. class TYPO:
  169.  
  170. def text_has_foobar( self, text ):
  171. return self._text_has_foobar_re_search( text ) is not None
  172. _text_has_foobar_re_search = _Re( r"""(?i)foobar""" ).search
  173.  
  174. TYPO = TYPO()
  175.  
  176. from TYPO import TYPO
  177. print( TYPO.text_has_foobar( 'FOObar ) )
  178.  
  179. import re
  180.  
  181. class Typo:
  182.  
  183. def text_has_foobar( self, text ):
  184. return re.compile( r"""(?i)foobar""" ).search( text ) is not None
  185.  
  186. typo = Typo()
  187. print( typo.text_has_foobar( 'FOObar ) )
Add Comment
Please, Sign In to add comment