advanced
Legacy Algorithm Optimization
Optimize an inefficient recursive algorithm for performance.
📝 Содержимое промпта
You are a Senior Performance Engineer. Analyze the following O(n^3) recursive function calculating Fibonacci numbers. Refactor the code to achieve O(n) time complexity using memoization or O(log n) using matrix exponentiation. Additionally, discuss potential stack overflow issues with deep recursion and how Tail Call Optimization (TCO) could theoretically apply, if supported by the language. Provide the refactored code in Python and a benchmark analysis comparing execution time for n=10,000.