⚡️ Speed up function chuliu_edmonds by 27%
#224
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.
📄 27% (0.27x) speedup for
chuliu_edmondsinstanza/models/common/chuliu_edmonds.py⏱️ Runtime :
61.5 milliseconds→48.4 milliseconds(best of104runs)📝 Explanation and details
Performance optimizations applied.
tarjan, use arrays for fast dependent lookup and state tracking; avoid repeated iterator allocation for dependents, so the function runs faster on large and dense graphs.process_cycle, usenp.ix_for high-performance submatrix extraction instead of chained advanced indexing, which is much more efficient for repeated large matrix use and avoids extra copies.np.count_nonzeroinstead of.sum()for boolean arrays inmaybe_pop_cyclefor more accurate intent and a potential micro-optimization.expand_contracted_treeavoids unnecessary traversals.All behaviors, names, comments, and signatures are preserved exactly as in the original.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-chuliu_edmonds-mh4gv4chand push.