⚡️ Speed up function pop_header_name by 13%
#379
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.
📄 13% (0.13x) speedup for
pop_header_nameinpandas/io/excel/_util.py⏱️ Runtime :
188 microseconds→166 microseconds(best of140runs)📝 Explanation and details
The optimization replaces list concatenation (
row[:i] + [""] + row[i + 1 :]) with unpacking syntax ([*row[:i], "", *row[i + 1 :]]) in the return statement. This change delivers a 13% speedup by eliminating the overhead of multiple list operations.Key Performance Improvement:
+operator, which requires multiple memory allocations and copying operations*) to build the result list in a single operation, reducing memory allocations and eliminating intermediate list creationWhy This Matters:
The function is called during Excel file parsing when handling MultiIndex headers, as shown in the function references. Since
pop_header_nameis invoked within loops over header rows and potentially for each column in multi-level headers, even small per-call improvements compound significantly during large file processing.Test Case Performance:
The optimization is particularly effective for larger datasets, where the original concatenation approach becomes increasingly expensive due to repeated memory allocation and copying of large list segments. This aligns well with pandas' typical use case of processing substantial data files.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pop_header_name-mihe9qphand push.