⚡️ Speed up function estimate_page_angle by 5%
#11
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.
📄 5% (0.05x) speedup for
estimate_page_angleindoctr/utils/geometry.py⏱️ Runtime :
1.97 milliseconds→1.88 milliseconds(best of184runs)📝 Explanation and details
The optimization replaces manual degree conversion with NumPy's built-in
np.degrees()function. Instead of multiplying by180 / np.pi, the code now usesnp.degrees(np.arctan(...)).Key Performance Improvement:
np.degrees()is a highly optimized C-level operation in NumPy that's faster than the explicit multiplication* 180 / np.piWhy This Works:
NumPy's
degrees()function is implemented in C and optimized for vectorized operations, making it more efficient than Python-level arithmetic operations. The constant180 / np.pimust be computed and then applied via multiplication, whilenp.degrees()performs this conversion directly in optimized native code.Test Case Performance:
The optimization shows consistent 3-8% improvements across most test cases, with particularly good results for:
The 5% overall speedup comes from this simple but effective replacement of manual trigonometric conversion with NumPy's optimized function, demonstrating how leveraging library-optimized operations can yield meaningful performance gains even in small code changes.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
common/test_utils_geometry.py::test_estimate_page_angle🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-estimate_page_angle-mg7sxmngand push.