You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The optimization achieves a **2180% speedup** by eliminating the most expensive operation in the original code: repeatedly calling `df.iloc[i]` to access DataFrame rows.
**Key Optimization: Vectorized Column Extraction**
The critical change replaces the inefficient row-by-row DataFrame access:
```python
# Original: Expensive row access (71.1% of total time)
for i in range(len(df)):
row = df.iloc[i] # This line alone took 244ms out of 344ms total
index_val = row[index]
column_val = row[columns]
value = row[values]
```
With direct NumPy array extraction and zip iteration:
```python
# Optimized: Extract entire columns as arrays once
index_arr = df[index].values # 2.4ms
columns_arr = df[columns].values # 1.3ms
values_arr = df[values].values # 1.3ms
# Then iterate over arrays directly
for index_val, column_val, value in zip(index_arr, columns_arr, values_arr):
```
**Why This Works**
1. **DataFrame.iloc[i] is extremely slow** - it creates a new Series object for each row access and involves significant pandas overhead for indexing operations
2. **Array access is fast** - NumPy arrays provide direct memory access with minimal overhead
3. **Bulk extraction is efficient** - Getting entire columns at once leverages pandas' optimized column operations
**Performance Impact by Test Case**
The optimization excels across all test scenarios:
- **Large-scale tests see massive gains**: 3543-6406% speedup for datasets with 1000+ rows
- **Medium datasets (100-900 rows)**: 1560-5350% speedup
- **Small datasets**: 57-129% speedup
- **Edge cases**: Generally 19-92% faster, though very small datasets (single row, empty) show minimal or slightly negative impact due to the overhead of array extraction
The optimization is particularly effective for scenarios with many rows since it eliminates the O(n) DataFrame row access overhead, making the algorithm scale much better with dataset size.
0 commit comments