⚡️ Speed up function sorter
by 9%
#105
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 bubble sort implementation with Python's built-in
arr.sort()
method, delivering significant performance improvements.Key Changes:
O(n²)
bubble sort loops with Python's nativearr.sort()
which uses Timsort (O(n log n)
)Why it's faster:
The line profiler shows the dramatic difference - the original code spent 98.8% of its time in the nested loops (lines with 2M+ hits), while the optimized version completes the sort in just 7.5% of total execution time. Python's Timsort is a highly optimized hybrid stable sorting algorithm implemented in C, making it orders of magnitude faster than a Python-based bubble sort.
Performance characteristics:
O(n²)
vs Timsort'sO(n log n)
)The 8% overall speedup in this test suite is conservative because most test cases use small arrays where the async sleep dominates runtime, but the algorithmic improvement provides substantial benefits for any non-trivial data sizes.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-sorter-mfelomug
and push.