⚡️ Speed up function regex_match
by 150%
#62
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 150% (1.50x) speedup for
regex_match
insrc/dsa/various.py
⏱️ Runtime :
3.50 milliseconds
→1.40 milliseconds
(best of236
runs)📝 Explanation and details
The optimized code achieves a 149% speedup through two key optimizations:
1. Pre-compilation of regex pattern
The original code calls
re.match(pattern, s)
inside the loop, which recompiles the regex pattern for every string comparison. The optimized version usesre.compile(pattern)
once before the loop, creating a compiled pattern object that can be reused efficiently. This eliminates redundant pattern parsing and compilation overhead.2. List comprehension instead of explicit loop
The optimized code replaces the explicit
for
loop with append operations with a list comprehension. List comprehensions are typically faster in Python due to optimized C-level iteration and reduced function call overhead.Performance analysis from line profiler:
re.match(pattern, s)
calls (40.3ms total)re.compile(pattern)
call (17.3ms total)Test case performance patterns:
The optimization is most effective for scenarios with multiple strings to match against the same pattern, where the one-time compilation cost is amortized across many match operations. For single-string matching or very small lists, the compilation overhead might not be worth it, but for typical use cases with multiple strings, this provides substantial performance gains.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-regex_match-mdpcermt
and push.