⚡️ Speed up function _addindent by 22%
#7
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.
📄 22% (0.22x) speedup for
_addindentindoctr/utils/repr.py⏱️ Runtime :
579 microseconds→475 microseconds(best of315runs)📝 Explanation and details
The optimization achieves a 22% speedup by eliminating redundant operations and reducing the number of intermediate variables:
Key Changes:
indent = " " * num_spacesis calculated upfront instead of repeatingnum_spaces * " "for every line in the list comprehension.pop(0), creating a new list, joining it, and then concatenating with the first line, the optimized version directly constructs the final result with"\n".join([s[0]] + [indent + line for line in s[1:]])firstand multiple reassignments tos.Why it's faster:
num_spaces * " "multiplication for each line (49.2% of total time), while the optimized version does it once.pop(0)operation and intermediate list reassignment.Performance characteristics from tests:
len(s) > 1, with minimal impact on single-line cases✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_addindent-mg7rh1z8and push.