⚡️ Speed up function _is_approx_fp by 32%
#51
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.
📄 32% (0.32x) speedup for
_is_approx_fpinquantecon/_compute_fp.py⏱️ Runtime :
238 microseconds→180 microseconds(best of250runs)📝 Explanation and details
The optimized code introduces a Numba-compiled function (
_numba_max_abs_diff) that replaces the NumPy operationnp.max(np.abs(result - v))in the performance-critical path. The key optimization is avoiding NumPy's temporary array creation and vectorized operations in favor of a simple loop that Numba can compile to fast machine code.Key optimizations:
@njit(fastmath=True, cache=True)decorator compiles the max absolute difference calculation to optimized machine code, eliminating Python overhead and enabling aggressive floating-point optimizations.np.abs(result - v)would generate, reducing memory allocation and cache pressure.Why this leads to speedup:
result - vandnp.abs(...)fastmath=Trueflag enables unsafe floating-point optimizations that can further accelerate the computationImpact on workloads:
Based on the
function_references, this function is called withincompute_fixed_point, which is likely used in iterative algorithms for economic modeling. The 32% speedup will compound across iterations, making fixed-point computations significantly faster. The test results show consistent 20-75% improvements across various array sizes and value ranges, with the largest gains on exact fixed points and large vectors.Test case performance:
The optimization performs best on exact matches (65-75% faster) and large arrays (25-55% faster), while maintaining correctness for edge cases like NaN/Inf values and non-array inputs through the fallback mechanism.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_is_approx_fp-mj9zqn5kand push.