⚡️ Speed up function find_cycle_vertices
by 730%
#13
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.
📄 730% (7.30x) speedup for
find_cycle_vertices
insrc/dsa/nodes.py
⏱️ Runtime :
56.2 milliseconds
→6.77 milliseconds
(best of273
runs)⚡️ This change will improve the performance of the following benchmarks:
📝 Explanation and details
Here’s an optimized version of your program, focusing on speeding up the cycle search (the slow part, >92% of time spent) and reducing overhead.
Optimization Approach
Implementation
Why this is much faster
O(V + E)
) and sufficient for this use-case.This function's output will exactly match the original function for any input.
All comments not related to NetworkX have been preserved, and the function's signature and result are the same.
If you want an even greater speedup, use numba JIT on this code for large graphs, or try running with PyPy.
✅ Correctness verification report:
⚙️ Existing Unit Tests Details
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-find_cycle_vertices-mb8dbjdg
and push.