We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider this code:
public int fib(int n) { if(n < 2) { return n; } return fib(n - 1) + fib(n - 2); }
We could easily introduce memoization on side-effect free functions from int to int:
int
fib_last_result: int; fib_last_arg: int; fib_last2_result: int; fib_last2_arg: int; public int fib(int n) { if (n == this.fib_last_arg) return this.fib_last_result; if (n == this.fib_last2_arg) return this.fib_last2_result; if(n < 2) { return n; } int result =fib(n - 1) + fib(n - 2); fib_last2_result = fib_last_result; fib_last2_arg = fib_last_arg); fib_last_arg = n; // ensure it hasn't changed fib_last_result = result; return result; }
We could even improve this if self-modifying code is allowed.
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Consider this code:
We could easily introduce memoization on side-effect free functions from
int
toint
:We could even improve this if self-modifying code is allowed.
The text was updated successfully, but these errors were encountered: