⚡️ Speed up function text_to_word_sequence by 45%#21
Open
codeflash-ai[bot] wants to merge 1 commit intomasterfrom
Open
⚡️ Speed up function text_to_word_sequence by 45%#21codeflash-ai[bot] wants to merge 1 commit intomasterfrom
text_to_word_sequence by 45%#21codeflash-ai[bot] wants to merge 1 commit intomasterfrom
Conversation
Here’s an optimized version of your `text_to_word_sequence` function. The main improvements are. - **Eliminate building the translate dict every call:** Instead, cache the translation table (`str.maketrans`) for the default `filters` and `split` values. - **Use generator expression with `filter` for filtering empty strings:** This is faster than a list comprehension. - **Short-circuit the `split` step for a single space:** When `split` is a space (the default), `str.split()` without an argument splits on any whitespace and skips empty strings, removing the need for post-filtering. **Key optimizations:** - The translation table and comprehension for the default case are only built once. - The main work of splitting and filtering is handled by the C-optimized `str.split()` when the split char is a space. - For non-default cases, extra processing is minimized. This should cut the runtime and memory allocations, especially for repeated calls using default parameters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 45% (0.45x) speedup for
text_to_word_sequenceinkeras/src/legacy/preprocessing/text.py⏱️ Runtime :
1.16 millisecond→805 microseconds(best of259runs)📝 Explanation and details
Here’s an optimized version of your
text_to_word_sequencefunction.The main improvements are.
str.maketrans) for the defaultfiltersandsplitvalues.filterfor filtering empty strings: This is faster than a list comprehension.splitstep for a single space: Whensplitis a space (the default),str.split()without an argument splits on any whitespace and skips empty strings, removing the need for post-filtering.Key optimizations:
str.split()when the split char is a space.This should cut the runtime and memory allocations, especially for repeated calls using default parameters.
✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-text_to_word_sequence-maxn1e67and push.