⚡️ Speed up function sorter
by 80%
#342
Closed
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.
📄 80% (0.80x) speedup for
sorter
incode_to_optimize/bubble_sort.py
⏱️ Runtime :
3.34 seconds
→1.85 seconds
(best of5
runs)📝 Explanation and details
Here is a greatly optimized version of your sorting function. You are currently using an unoptimized bubble sort, which is both time- and cache-inefficient for large lists. There are several ways to improve its performance without changing the function signature or output.
sort()
, which is highly optimized (Timsort; O(n log n)).len(arr)
calls in the loop.Below, option 1 uses built-in sort (fastest in practice), and option 2 is an optimized in-place bubble sort in case you need to keep the bubble sort code style.
Option 1: Use Python’s built-in sort (Recommended, unless manual sort required)
Option 2: Optimized Bubble Sort (If you want to keep the basic algorithm)
Notes on optimization:
Recommendation
Either will be vastly faster than your original code.
Your main slowness was due to the O(n²) inefficient bubble sort.
Let me know if you need it adapted for e.g. descending order or for specific data types!
✅ Correctness verification report:
benchmarks/test_benchmark_bubble_sort.py
test_bubble_sort.py
test_bubble_sort_conditional.py
test_bubble_sort_import.py
test_bubble_sort_in_class.py
test_bubble_sort_parametrized.py
test_bubble_sort_parametrized_loop.py
benchmarks/test_benchmark_bubble_sort.py
test_sort2
test_bubble_sort.py
test_sort
test_bubble_sort_conditional.py
test_sort
test_bubble_sort_import.py
test_sort
test_bubble_sort_in_class.py
TestSorter.test_sort_in_pytest_class
test_bubble_sort_parametrized.py
test_sort_parametrized
test_bubble_sort_parametrized_loop.py
test_sort_loop_parametrized
📊 Bubble Sort Benchmark Improvements
benchmarks/test_benchmark_bubble_sort.py
test_sort2
test_bubble_sort.py
test_sort
test_bubble_sort_conditional.py
test_sort
test_bubble_sort_import.py
test_sort
test_bubble_sort_in_class.py
TestSorter.test_sort_in_pytest_class
test_bubble_sort_parametrized.py
test_sort_parametrized
test_bubble_sort_parametrized_loop.py
test_sort_loop_parametrized
🌀 Generated Regression Tests Details and Performance Breakdown
To edit these changes
git checkout codeflash/optimize-sorter-mc13udav
and push.