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.

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to