Skip to content

⚡ Optimize mortgage payoff performance using NPER formula#18

Merged
LimaniBhavik merged 1 commit into
mainfrom
performance-opt-mortgage-payoff-nper-formula-18190969366061001921
Mar 1, 2026
Merged

⚡ Optimize mortgage payoff performance using NPER formula#18
LimaniBhavik merged 1 commit into
mainfrom
performance-opt-mortgage-payoff-nper-formula-18190969366061001921

Conversation

@LimaniBhavik

Copy link
Copy Markdown
Owner

Replaced the simulation-based iterative loop in the mortgage_payoff function with the closed-form NPER (number of periods) formula using logarithms.

This change improves the calculation performance from O(N) to O(1) complexity, achieving an approximately 40x speedup in benchmark tests (measured 9.5ms vs 377ms for 100,000 iterations).

The new implementation:

  • Corrects handling for 0% interest scenarios.
  • Preserves the existing early-exit behavior (returning 9999) for non-paying loans.
  • Maintains the 1200-month safety limit (returning 1201) for compatibility with the previous simulation logic.
  • Adds an example benchmark file for future performance monitoring.

Replaced the simulation-based iterative loop in the `mortgage_payoff`
function with the closed-form NPER (number of periods) formula using
logarithms.

This change improves the calculation performance from O(N) to O(1)
complexity, achieving an approximately 40x speedup in benchmark
tests (measured 9.5ms vs 377ms for 100,000 iterations).

The new implementation:
- Corrects handling for 0% interest scenarios.
- Preserves the existing early-exit behavior (returning 9999) for
  non-paying loans.
- Maintains the 1200-month safety limit (returning 1201) for
  compatibility with the previous simulation logic.
- Adds an example benchmark file for future performance monitoring.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
@LimaniBhavik LimaniBhavik merged commit 8363d0c into main Mar 1, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant