⚡️ Speed up function is_palindrome by 44%
#63
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.
📄 44% (0.44x) speedup for
is_palindromeinsrc/dsa/various.py⏱️ Runtime :
699 microseconds→486 microseconds(best of956runs)📝 Explanation and details
The optimized code achieves a 43% speedup by eliminating the expensive string preprocessing step and using a two-pointer approach that processes characters in-place.
Key Optimization: Eliminated String Construction
The original code creates an entirely new cleaned string using
"".join(c.lower() for c in text if c.isalnum()), which is the performance bottleneck (58.6% of total time). This involves:The optimized version eliminates this preprocessing entirely by using two pointers that skip non-alphanumeric characters on-the-fly during comparison.
Two-Pointer In-Place Processing
Instead of creating a cleaned string, the optimized code:
leftandrightpointers starting from string endswhileloops.lower()only when neededPerformance Analysis by Test Case Type:
Massive speedup for early-exit cases: Non-palindromes see 200-8500% speedups (e.g., "python" 300% faster, large non-palindromes 7500+ % faster) because the two-pointer approach can return
Falseafter checking just the first differing pair, while the original must still process the entire string upfront.Moderate speedup for true palindromes: True palindromes see 30-85% speedups because both approaches must check most/all characters, but the optimized version avoids the preprocessing overhead and string allocation costs.
Slight slowdown for non-alphanumeric heavy strings: Cases with many consecutive non-alphanumeric characters (like "!@#$%^&*()") can be 12-62% slower because the pointer advancement loops add overhead when there are few actual character comparisons to perform.
Memory Efficiency: The optimized approach uses O(1) additional space versus O(n) for creating the cleaned string, reducing memory pressure and cache misses.
The optimization is most effective for inputs with early mismatches or moderate amounts of non-alphanumeric characters, making it ideal for real-world palindrome checking where most inputs are not palindromes.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-is_palindrome-mdpcje5vand push.