⚡️ Speed up function cosine_similarity by 159%
#159
Closed
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.
📄 159% (1.59x) speedup for
cosine_similarityinsrc/statistics/similarity.py⏱️ Runtime :
31.7 milliseconds→12.2 milliseconds(best of134runs)📝 Explanation and details
Optimizations made:
np.asarray(..., dtype=np.float64)to minimize unnecessary data copying if already ndarray of correct type.np.errstatecontext so that no warning/error is raised and doesn't mask global warning state.np.isnan(similarity) | np.isinf(similarity)by using~np.isfinite(similarity)directly and only creating the mask array if needed.Matrix) for improved readability and clarity.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_odeed6fh/tmp_mpz2bjo/test_concolic_coverage.py::test_cosine_similaritycodeflash_concolic_odeed6fh/tmp_mpz2bjo/test_concolic_coverage.py::test_cosine_similarity_2codeflash_concolic_odeed6fh/tmp_mpz2bjo/test_concolic_coverage.py::test_cosine_similarity_3To edit these changes
git checkout codeflash/optimize-cosine_similarity-mhf5oewsand push.