Commit 6b72059
authored
⚡️ Speed up function
The optimized code achieves a **15.9x speedup** by replacing explicit nested loops with vectorized NumPy operations, specifically using `np.dot()` for computing dot products.
**Key Optimizations Applied:**
1. **Vectorized dot products for U matrix computation**: Instead of the nested loop `for j in range(i): sum_val += L[i, j] * U[j, k]`, the optimized version uses `np.dot(Li, U[:i, k])` where `Li = L[i, :i]`.
2. **Pre-computed slices for L matrix computation**: The optimized version extracts `Ui = U[:i, i]` once per iteration and reuses it with `np.dot(L[k, :i], Ui)` instead of recalculating the sum in a loop.
**Why This Creates Significant Speedup:**
The original implementation has O(n³) scalar operations performed in Python loops. From the line profiler, we can see that the innermost loop operations (`sum_val += L[i, j] * U[j, k]` and `sum_val += L[k, j] * U[j, i]`) account for **60.9%** of total runtime (30.7% + 30.2%).
The optimized version leverages NumPy's highly optimized BLAS (Basic Linear Algebra Subprograms) routines for dot products, which:
- Execute in compiled C code rather than interpreted Python
- Use vectorized CPU instructions (SIMD)
- Have better memory access patterns and cache locality
**Performance Characteristics by Test Case:**
- **Small matrices (≤10x10)**: The optimization shows **38-47% slower performance** due to NumPy function call overhead dominating the small computation cost
- **Medium matrices (50x50)**: Shows **3-6x speedup** where vectorization benefits start outweighing overhead
- **Large matrices (≥100x100)**: Demonstrates **7-15x speedup** where vectorized operations provide maximum benefit
The crossover point appears around 20-30x30 matrices, making this optimization particularly effective for larger matrix decompositions commonly encountered in scientific computing and machine learning applications.matrix_decomposition_LU by 1,015%1 parent 9b951ff commit 6b72059
1 file changed
+5
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
| 64 | + | |
63 | 65 | | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
| 66 | + | |
68 | 67 | | |
| 68 | + | |
69 | 69 | | |
70 | | - | |
71 | | - | |
72 | | - | |
| 70 | + | |
73 | 71 | | |
74 | 72 | | |
75 | 73 | | |
| |||
0 commit comments