Skip to content

perf(metrics): vectorize global_efficiency inner loop - #51

Merged
cbueth merged 3 commits into
mainfrom
perf/vectorize-global-efficiency
Jul 30, 2026
Merged

perf(metrics): vectorize global_efficiency inner loop#51
cbueth merged 3 commits into
mainfrom
perf/vectorize-global-efficiency

Conversation

@cbueth

@cbueth cbueth commented Jul 30, 2026

Copy link
Copy Markdown
Owner

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 / distance in a single vectorised operation. Now it uses np.sum, not +=.

Also replaces the fragile test_global_efficiency_pair_count_zero test (which mocked builtins.range as an implementation detail of the loop) with test_global_efficiency_all_paths_infinite that 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.

cbueth added 2 commits July 30, 2026 17:31
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.
@cbueth cbueth self-assigned this Jul 30, 2026
@cbueth cbueth added the enhancement New feature or request label Jul 30, 2026
@codspeed-hq

codspeed-hq Bot commented Jul 30, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 36.75%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 4 improved benchmarks
✅ 24 untouched benchmarks

Performance Changes

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)

Open in CodSpeed

@codecov

codecov Bot commented Jul 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (875adcd) to head (592d2fc).

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cbueth
cbueth merged commit a690e46 into main Jul 30, 2026
12 checks passed
@cbueth
cbueth deleted the perf/vectorize-global-efficiency branch July 30, 2026 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant