⚡️ Speed up function find_last_node by 16,136%#277
Closed
codeflash-ai[bot] wants to merge 1 commit intooptimizefrom
Closed
⚡️ Speed up function find_last_node by 16,136%#277codeflash-ai[bot] wants to merge 1 commit intooptimizefrom
find_last_node by 16,136%#277codeflash-ai[bot] wants to merge 1 commit intooptimizefrom
Conversation
The optimized code achieves a **162x speedup** (from 57ms to 351μs) by eliminating a nested loop anti-pattern that caused quadratic time complexity.
**Key Algorithmic Improvement:**
The original implementation uses a nested comprehension: for each node, it scans through *all* edges to check if that node is a source. This creates O(n × m) complexity where n = number of nodes and m = number of edges.
The optimized version **preprocesses edges once** into a set of source IDs (`sources = {e["source"] for e in edges}`), then performs O(1) set membership checks for each node. This reduces complexity to O(n + m).
**Why This Matters:**
The test results demonstrate dramatic improvements in real-world scenarios:
- **Linear chains** (1000 nodes): 17.5ms → 47.6μs (**367x faster**) - The original code performed ~1 million edge checks; the optimized version does just 1000 set lookups
- **Dense graphs** (100 nodes, 4950 edges): 11.1ms → 85.8μs (**129x faster**) - Avoided 495,000 edge comparisons
- **Early termination cases**: When the sink appears early in the node list, the optimized version benefits immediately from the O(1) lookup without rescanning edges
**Performance Characteristics:**
The optimization excels when:
- Edge count is large (more comparisons avoided)
- The target node appears late in the list or there are many nodes
- Even with small inputs (10-20 nodes), speedups range from 80-200% due to the efficiency of set operations vs repeated iterations
The single upfront cost of building the set (typically <50μs for 1000 edges) is amortized across all node checks, making this universally faster for any non-trivial graph.
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.
📄 16,136% (161.36x) speedup for
find_last_nodeinsrc/algorithms/graph.py⏱️ Runtime :
57.0 milliseconds→351 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 162x speedup (from 57ms to 351μs) by eliminating a nested loop anti-pattern that caused quadratic time complexity.
Key Algorithmic Improvement:
The original implementation uses a nested comprehension: for each node, it scans through all edges to check if that node is a source. This creates O(n × m) complexity where n = number of nodes and m = number of edges.
The optimized version preprocesses edges once into a set of source IDs (
sources = {e["source"] for e in edges}), then performs O(1) set membership checks for each node. This reduces complexity to O(n + m).Why This Matters:
The test results demonstrate dramatic improvements in real-world scenarios:
Performance Characteristics:
The optimization excels when:
The single upfront cost of building the set (typically <50μs for 1000 edges) is amortized across all node checks, making this universally faster for any non-trivial graph.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-find_last_node-mlfeu145and push.