[Python-ideas] No need to add a regex pattern literal

Ma Lin malincns at 163.com
Tue Jan 1 21:51:13 EST 2019


On 19-1-1 21:39, Stefan Behnel wrote:
 > I wouldn't be surprised if the slowest part here was the isinstance()
 > check. Maybe the RegexFlag class could implement "__hash__()" as "return
 > hash(self.value)" ?

Apply this patch:

  def _compile(pattern, flags):
      # internal: compile pattern
-    if isinstance(flags, RegexFlag):
-        flags = flags.value
+    try:
+        flags = int(flags)
+    except:
+        pass
      try:
          return _cache[type(pattern), pattern, flags]
      except KeyError:

Then run this benchmark on my Raspberry Pi 3B:

import perf
runner = perf.Runner()
runner.timeit(name="compile_re",
               stmt="re.compile(b'[^0-9A-F]')",
               setup="import re")

Mean +- std dev: [a] 7.71 us +- 0.09 us -> [b] 6.74 us +- 0.10 us: 1.14x 
faster (-13%)

Looks great.



More information about the Python-ideas mailing list