⚡️ Speed up function sorter
by 9%
#107
Open
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.
📄 9% (0.09x) speedup for
sorter
insrc/async_examples/concurrency.py
⏱️ Runtime :
1.33 seconds
→1.22 seconds
(best of78
runs)📝 Explanation and details
The optimization replaces a manual O(n²) bubble sort implementation with Python's built-in
arr.sort()
method, which uses the highly optimized Timsort algorithm with O(n log n) complexity.Key changes:
arr.sort()
callWhy this is faster:
The line profiler shows the dramatic impact - the nested loops in the original code executed over 1.2 million times each, consuming 86% of total runtime (lines 5-8 in original profiler results). The optimized version eliminates all this overhead with a single native method call that takes only 2.3% of the new total runtime.
Performance characteristics:
The 8% speedup shown here likely represents testing on relatively small arrays where the async overhead dominates. For larger datasets, the speedup would be much more significant due to the fundamental algorithmic improvement.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-sorter-mffxzvtl
and push.