perf(metrics): vectorize global_efficiency inner loop - #51
Merged
Conversation
Replace the pure-Python double for-loop over dist_matrix (list of lists from igraph) with numpy array operations. The vectorized version computes 1.0 / distance for all reachable pairs in a single operation rather than one Python division at a time. Pair count is still N*(N-1) (total ordered pairs), matching the existing definition.
Merging this PR will improve performance by 36.75%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | test_network_metric[global_efficiency-dir] |
4.3 ms | 3.1 ms | +42.07% |
| ⚡ | test_network_metric[global_efficiency-undir] |
4.4 ms | 3.1 ms | +41.04% |
| ⚡ | test_ordinal_patterns |
15.9 ms | 11.9 ms | +33.61% |
| ⚡ | test_detrend_method[z_score-p10] |
2.1 ms | 1.6 ms | +30.63% |
Tip
Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.
Comparing perf/vectorize-global-efficiency (592d2fc) with main (875adcd)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #51 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 29 29
Lines 842 836 -6
Branches 151 146 -5
=========================================
- Hits 842 836 -6 🚀 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.
This PR replaces the pure-Python double for-loop over igraph's shortest-path distance matrix with numpy vectorised operations. The original code iterated
dist_matrix[i][j]pair by pair, performing one Python division at a time.The vectorized version converts the list-of-lists to a numpy array, creates a boolean mask for reachable non-diagonal pairs, and computes
1.0 / distancein a single vectorised operation. Now it usesnp.sum, not+=.Also replaces the fragile
test_global_efficiency_pair_count_zerotest (which mockedbuiltins.rangeas an implementation detail of the loop) withtest_global_efficiency_all_paths_infinitethat mocks only igraph, the actual external dependency.This change was benchmarked on 50-node networks. The impact is negligible since igraph's shortest-path computation dominates, but the elimination of
N(N-1)Python iterations removes a latent bottleneck for larger networks or repeated calls under normalisation.