Skip to content

perf: vectorize distance calculations using math.dist (~13x speedup)#537

Merged
rolan701 merged 2 commits into
hjkgrp:mainfrom
sniderg:perf-distance-optimize
Apr 20, 2026
Merged

perf: vectorize distance calculations using math.dist (~13x speedup)#537
rolan701 merged 2 commits into
hjkgrp:mainfrom
sniderg:perf-distance-optimize

Conversation

@sniderg
Copy link
Copy Markdown
Contributor

@sniderg sniderg commented Mar 24, 2026

Summary

This PR optimizes the core distance() function in molSimplify/Scripts/geometry.py. By replacing the previous implementation (which instantiated numpy arrays for every call) with Python's built-in, C-backed math.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):

  • Original Implementation: 0.2528 seconds
  • Optimized (math.dist): 0.0199 seconds
  • Speedup: ~12.7x

This change is a drop-in, transparent optimization that passes all existing 212 tests in tests/test_geometry.py and tests/test_mol3D.py.

Changes

  • Refactored molSimplify/Scripts/geometry.py to use math.dist.
  • Added molSimplify/benchmark_distance.py to allow maintainers to verify these gains on their own hardware.

Comment thread benchmark_distance.py
import time
import numpy as np
import math
from scipy.spatial.distance import pdist, cdist
Comment thread benchmark_distance.py
# 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
Copy link
Copy Markdown

codecov Bot commented Apr 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 40.25%. Comparing base (44c0b8e) to head (79f0084).
⚠️ Report is 6 commits behind head on main.

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     
Flag Coverage Δ
unittests 40.25% <100.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rolan701 rolan701 merged commit db0e4bf into hjkgrp:main Apr 20, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants