⚡️ Speed up method Epoch._cmp by 15%
#237
Open
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.
📄 15% (0.15x) speedup for
Epoch._cmpinlib/matplotlib/testing/jpl_units/Epoch.py⏱️ Runtime :
7.68 milliseconds→6.68 milliseconds(best of49runs)📝 Explanation and details
The optimized code achieves a 14% speedup through two key performance improvements:
1. Class-level
alloweddictionary (Major optimization)allowedframe conversion dictionary from instance-level to class-level as a static attributeconvert()callsconvert()improved from 4.92ms to 4.67ms total time, with thereturn Epoch(...)line dropping from 4.39ms to 4.10ms per hit2.
divmod()optimization in__init__normalizationmath.floor(self._seconds / 86400)and modulus operations with a singledivmod(sec, 86400.0)calldivmod()computes both quotient and remainder in one operation, avoiding duplicate division computation and an extra multiplication (deltaDays * 86400.0)Performance characteristics from test results:
_cmpcalls with different frames)The optimizations are particularly effective for:
Epochobjects are frequently instantiated_cmp()is called repeatedly, as it triggersconvert()for cross-frame operationsThe changes preserve all existing behavior, validation logic, and API compatibility while reducing both memory footprint and computational overhead.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-Epoch._cmp-mj9vpssiand push.