⚡️ Speed up function cosine_similarity by 225%
#157
Closed
+10
−4
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.
📄 225% (2.25x) speedup for
cosine_similarityinsrc/statistics/similarity.py⏱️ Runtime :
29.1 milliseconds→8.96 milliseconds(best of324runs)📝 Explanation and details
The optimization achieves a 224% speedup through three key changes:
1. Avoiding Array Copy Operations:
np.array()withnp.asarray()+ explicitdtype=np.float64specificationnp.asarrayperforms zero-copy conversion when input is already a compatible numpy array, whilenp.arrayalways creates a new copy2. Eliminating
np.outer()with Broadcasting:np.dot(X, Y.T) / np.outer(X_norm, Y_norm)with separatedotand broadcasting-baseddenomcalculationsnp.outercreates an explicit 2D matrix in memory, while broadcasting (X_norm[:, None] * Y_norm[None, :]) computes the same result without materializing the full matrix until needed3. Optimized NaN/Inf Handling:
~np.isfinite()instead of separatenp.isnan() | np.isinf()checksnp.errstatecontext manager to suppress division warnings more efficientlyPerformance by Test Case:
The optimizations are most effective for larger matrices and scenarios involving zero vectors or invalid operations, while maintaining identical behavior and numerical accuracy.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_pr0pvdtm/tmp2tcbmx60/test_concolic_coverage.py::test_cosine_similaritycodeflash_concolic_pr0pvdtm/tmp2tcbmx60/test_concolic_coverage.py::test_cosine_similarity_2codeflash_concolic_pr0pvdtm/tmp2tcbmx60/test_concolic_coverage.py::test_cosine_similarity_3To edit these changes
git checkout codeflash/optimize-cosine_similarity-mhd3u5iiand push.