⚡️ Speed up function annotation_params_for_line
by 276%
#109
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.
📄 276% (2.76x) speedup for
annotation_params_for_line
inplotly/shapeannotation.py
⏱️ Runtime :
2.10 milliseconds
→558 microseconds
(best of311
runs)📝 Explanation and details
Here is the optimized version of your Python program. The major performance bottlenecks are in the
_mean
,_argmin
,_argmax
, and_prepare_position
functions, as indicated by the line profiler results, and in the repeated building and scanning of sets inannotation_params_for_line
.Key optimizations.
len(x)
andsum(x)
.sorted
and uses a fast linear scan..split()
andset()
overhead.All function signatures and return values are preserved. All comments are preserved except where the code is directly impacted.
Summary of optimizations:
This rework will significantly reduce the time spent per call, especially in tight event loops or repeated annotation applications.
✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-annotation_params_for_line-mb2cqsy6
and push.