⚡️ Speed up method DiGraph._condensation_lil by 80%#107
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
⚡️ Speed up method DiGraph._condensation_lil by 80%#107codeflash-ai[bot] wants to merge 1 commit intomainfrom
DiGraph._condensation_lil by 80%#107codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The optimized code achieves an 80% speedup by replacing Python-level iteration with vectorized NumPy operations when processing sparse CSR matrices. **Key Optimizations:** 1. **Vectorized CSR Matrix Traversal**: The original `_csr_matrix_indices()` uses nested Python `for` loops to yield matrix indices one-by-one. The optimized version uses `np.repeat()` and `np.diff()` to directly extract all row/column index pairs in vectorized operations, eliminating Python loop overhead entirely. 2. **Batch Processing in `_condensation_lil()`**: Instead of iterating through edges one-at-a-time and setting sparse matrix entries individually, the optimized version: - Extracts all edges at once using vectorized operations - Maps nodes to their SCC labels using NumPy fancy indexing - Filters edges within the same SCC using boolean masking - Uses `np.unique()` to deduplicate edges between SCCs in one operation - Sets all condensation edges at once via fancy indexing **Why This is Faster:** - **Eliminates Python Loop Overhead**: Python loops are expensive compared to compiled NumPy operations. The original code calls Python's `yield` statement for each edge, while the optimized version processes all edges in bulk. - **Memory Locality**: Vectorized operations have better cache performance and allow NumPy's optimized C code to run without Python interpreter overhead. - **Reduced Function Calls**: Setting sparse matrix entries in bulk is much faster than individual assignments in a loop. **Performance Characteristics:** The test results show the optimization is particularly effective for: - **Large sparse graphs** (500-1000 nodes): 137-281% speedup, as vectorization benefits scale with input size - **Graphs with many edges between SCCs**: Dense condensations benefit most from batch deduplication via `np.unique()` For very small graphs (<10 nodes), the optimization shows 12-57% *slowdown* due to NumPy array creation overhead dominating the computation. However, since graph algorithms typically target larger datasets where this optimization shines, the trade-off is worthwhile for production workloads. **Impact on Workloads:** This optimization benefits any code analyzing graph structure properties (strongly connected components, condensation graphs) on moderate-to-large directed graphs, which is common in network analysis, Markov chain analysis, and related computational economics applications.
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.
📄 80% (0.80x) speedup for
DiGraph._condensation_lilinquantecon/_graph_tools.py⏱️ Runtime :
9.55 milliseconds→5.30 milliseconds(best of110runs)📝 Explanation and details
The optimized code achieves an 80% speedup by replacing Python-level iteration with vectorized NumPy operations when processing sparse CSR matrices.
Key Optimizations:
Vectorized CSR Matrix Traversal: The original
_csr_matrix_indices()uses nested Pythonforloops to yield matrix indices one-by-one. The optimized version usesnp.repeat()andnp.diff()to directly extract all row/column index pairs in vectorized operations, eliminating Python loop overhead entirely.Batch Processing in
_condensation_lil(): Instead of iterating through edges one-at-a-time and setting sparse matrix entries individually, the optimized version:np.unique()to deduplicate edges between SCCs in one operationWhy This is Faster:
yieldstatement for each edge, while the optimized version processes all edges in bulk.Performance Characteristics:
The test results show the optimization is particularly effective for:
np.unique()For very small graphs (<10 nodes), the optimization shows 12-57% slowdown due to NumPy array creation overhead dominating the computation. However, since graph algorithms typically target larger datasets where this optimization shines, the trade-off is worthwhile for production workloads.
Impact on Workloads:
This optimization benefits any code analyzing graph structure properties (strongly connected components, condensation graphs) on moderate-to-large directed graphs, which is common in network analysis, Markov chain analysis, and related computational economics applications.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-DiGraph._condensation_lil-mkp362siand push.