⚡️ Speed up function sorter by 174,908%
#344
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.
📄 174,908% (1,749.08x) speedup for
sorterincode_to_optimize/bubble_sort.py⏱️ Runtime :
3.39 seconds→1.94 milliseconds(best of491runs)📝 Explanation and details
Here’s your optimized program. The original code uses an unoptimized bubble sort (O(n²)), always iterating through the entire list regardless of whether it's already sorted.
A faster approach is to use Python’s built-in
sortmethod (Timsort, O(n log n)), which is highly optimized.I’ve preserved your print statements and function signature.
Comments:
sorted(arr)(returns a new list).Let me know if you need to preserve the original or add more features!
✅ Correctness verification report:
⚙️ Existing Unit Tests Details and Runtime
benchmarks/test_benchmark_bubble_sort.pytest_sort26.97ms16.5μs✅42042.47%test_bubble_sort.pytest_sort825ms139μs✅592141.14%test_bubble_sort_conditional.pytest_sort6.00μs2.96μs✅102.84%test_bubble_sort_import.pytest_sort832ms141μs✅588624.04%test_bubble_sort_in_class.pyTestSorter.test_sort_in_pytest_class831ms138μs✅602634.76%test_bubble_sort_parametrized.pytest_sort_parametrized507ms136μs✅371076.70%test_bubble_sort_parametrized_loop.pytest_sort_loop_parametrized95.8μs20.5μs✅366.60%🌀 Generated Regression Tests Details and Runtime
To edit these changes
git checkout codeflash/optimize-sorter-mc1a8nzeand push.