⚡️ Speed up function dataframe_merge
by 1,072%
#69
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.
📄 1,072% (10.72x) speedup for
dataframe_merge
insrc/numpy_pandas/dataframe_operations.py
⏱️ Runtime :
1.69 seconds
→144 milliseconds
(best of63
runs)📝 Explanation and details
The optimized code achieves a 1071% speedup by replacing slow pandas
.iloc[]
operations with fast NumPy array indexing. Here are the key optimizations:1. NumPy Array Access Instead of .iloc[]
right.iloc[i][right_on]
andleft.iloc[i]
for data access, which are extremely slow pandas operationsleft.values
,right.values
) and used direct array indexing likeright_values[i, right_on_idx]
right.iloc[right_idx]
took 60.4% of total time in the original (8.32s), while the equivalent NumPy operations are barely visible in the optimized version2. Pre-computed Column Index Mappings
left_row[col]
andright_row[col]
left_col_indices
,right_col_indices
) and used direct array indexing:left_values[i, left_col_indices[col]]
3. Direct Column Index Lookup
columns.get_loc()
to get integer indices upfront, enabling direct NumPy array accessWhy This Works:
.iloc[]
has significant overhead for type checking, alignment, and Series creationTest Case Performance:
The optimizations are most effective for:
test_large_scale_many_duplicates
shows 753% speedup - the more data accessed, the greater the NumPy advantage.iloc[]
calls benefit most from the NumPy conversionThe optimization maintains identical functionality while dramatically reducing the computational overhead of data access operations.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-dataframe_merge-mdpei80l
and push.