⚡️ Speed up function find_common_tags by 64%#306
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
⚡️ Speed up function find_common_tags by 64%#306codeflash-ai[bot] wants to merge 1 commit intomainfrom
find_common_tags by 64%#306codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
Reduced runtime from 11.3ms to 6.88ms (64% faster) by initializing the common set from the article with the fewest tags and avoiding the articles[1:] slice. Because set.intersection_update costs scale with the size of the left-hand set and slicing allocates a new list, starting from the smallest tag set cuts the amount of work and memory churn — the line profiler shows intersection_update’s share of time falling from ~54.8% to ~34.8% and the routine-level profile roughly halving. The trade-off is one extra O(n) scan to find the smallest tag list and a per-iteration skip check, which can regress on inputs consisting of many articles with uniformly tiny tag lists (observed ~15% slower in a couple large-uniform benchmarks), but it delivers clear wins for imbalanced or large tag collections.
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.
📄 64% (0.64x) speedup for
find_common_tagsinsrc/algorithms/string.py⏱️ Runtime :
11.3 milliseconds→6.88 milliseconds(best of71runs)📝 Explanation and details
Reduced runtime from 11.3ms to 6.88ms (64% faster) by initializing the common set from the article with the fewest tags and avoiding the articles[1:] slice. Because set.intersection_update costs scale with the size of the left-hand set and slicing allocates a new list, starting from the smallest tag set cuts the amount of work and memory churn — the line profiler shows intersection_update’s share of time falling from ~54.8% to ~34.8% and the routine-level profile roughly halving. The trade-off is one extra O(n) scan to find the smallest tag list and a per-iteration skip check, which can regress on inputs consisting of many articles with uniformly tiny tag lists (observed ~15% slower in a couple large-uniform benchmarks), but it delivers clear wins for imbalanced or large tag collections.
✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_common_tags.py::test_common_tags_1🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-find_common_tags-mn00300fand push.