perf: vectorize distance calculations using math.dist (~13x speedup)#537
Merged
Conversation
| import time | ||
| import numpy as np | ||
| import math | ||
| from scipy.spatial.distance import pdist, cdist |
| # 3. Fully vectorized with scipy pdist | ||
| start_time = time.time() | ||
| coords_array = np.array(coords) # Include array conversion overhead | ||
| vec_distances = pdist(coords_array) |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #537 +/- ##
==========================================
- Coverage 40.26% 40.25% -0.02%
==========================================
Files 93 93
Lines 30325 30324 -1
==========================================
- Hits 12211 12206 -5
- Misses 18114 18118 +4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Summary
This PR optimizes the core
distance()function inmolSimplify/Scripts/geometry.py. By replacing the previous implementation (which instantiatednumpyarrays for every call) with Python's built-in, C-backedmath.dist, we achieve a significant performance boost for all coordinate-heavy operations (e.g., graph generation, overlap checks, and structure generation).Benchmarks
Tested on a simulated system of 500 atoms (124,750 pairwise distance calculations):
math.dist): 0.0199 secondsThis change is a drop-in, transparent optimization that passes all existing 212 tests in
tests/test_geometry.pyandtests/test_mol3D.py.Changes
molSimplify/Scripts/geometry.pyto usemath.dist.molSimplify/benchmark_distance.pyto allow maintainers to verify these gains on their own hardware.