Commit a8b7550
authored
⚡️ Speed up function
The optimization replaces the manual bubble sort implementation with Python's built-in `arr.sort()` method, resulting in a **35% speedup**.
**Key Changes:**
- Eliminated the nested `for` loops that implemented bubble sort (O(n²) complexity)
- Replaced with `arr.sort()` which uses Timsort algorithm (O(n log n) complexity)
- Removed manual element swapping logic
**Why This Is Faster:**
1. **Algorithm efficiency**: Bubble sort requires O(n²) comparisons and swaps, while Timsort is O(n log n) on average
2. **Native implementation**: Python's `sort()` is implemented in optimized C code, avoiding Python interpreter overhead for the sorting logic
3. **Reduced function calls**: The line profiler shows the original code made 130+ hits across sorting lines, while the optimized version makes only 2 hits to `arr.sort()`
**Performance Impact:**
- Original: 50 microseconds total time with heavy computation in nested loops
- Optimized: 33 microseconds total time with minimal sorting overhead
- The sorting operation itself dropped from ~16 microseconds (multiple loop iterations) to just 2 microseconds
This optimization is particularly effective for larger arrays where the O(n²) vs O(n log n) difference becomes more pronounced, though even small arrays benefit from the native C implementation speed.mysorter by 36%1 parent 1d8b1ed commit a8b7550
1 file changed
+4
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
| 3 | + | |
9 | 4 | | |
10 | 5 | | |
11 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
0 commit comments