You are developing a Python application where a certain function’s output is dependent on expensive computation. How would you use decorators to optimize this scenario?

  • Create a decorator function that caches the function's output using a dictionary.
  • Create a decorator function that logs function arguments and return values.
  • Create a decorator function that raises an exception if the function takes too long to execute.
  • Create a decorator function that replaces the function with a faster implementation.
To optimize a function with expensive computation, you can use a decorator that caches the function's output, preventing redundant computations. This is known as memoization and is commonly used for optimization.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *