Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A Python Decorator similar to but simpler than functools.lru_cache with the extra ability to select parameters

Notifications You must be signed in to change notification settings

neeru1207/Selective_LRU_Cache_Decorator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

12 Commits

Repository files navigation

Selective_LRU_Cache_Decorator

A Python Decorator similar to but much simpler than functools.lru_cache with the extra ability to select parameters. It can be used to memoize recursive functions.

Usage

  • Clone this repository and place the lru_cache module in the working directory.

  • Import the LRU Cache in your Python code by typing the below command.

    from lru_cache.lru_cache import SelectiveLRUCache
  • Decorate a function with the cache by placing the below command just above the function definition. The decorator takes two parameters - maxsize (the maximum size of the LRU Cache) and parameters. The "parameters" parameter is a lambda function which decides which parameters to select. In the below example, the "parameters" lambda selects only the first parameter.

    @SelectiveLRUCache(parameters=lambda x:(x[0],), maxsize=None)
    def fibonacci(n, cntr):
  • The "parameters" lambda function takes the list of parameters as input and outputs a tuple of the selected list of parameters. Here the lambda selects the first and third parameters from the list of parameters.

    @SelectiveLRUCache(parameters=lambda x:(x[0], x[2]))

Performance

  • When tested on the recursive fibonacci function, a 46000 times faster execution and a 29000 times reduction in the number of function calls was achieved.

  • Fibonacci(40) without the cache took 331160289 function calls and 92.686 seconds as shown below:

  • Fibonacci(40) with the cache took 1140 function calls and only 0.002 seconds to execute as shown below:

Contributing

  • Feel free to open an issue if any bug is found.
  • Pull requests are welcome. Just make sure to contribute readable, well commented, and tested code.

About

A Python Decorator similar to but simpler than functools.lru_cache with the extra ability to select parameters

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

AltStyle によって変換されたページ (->オリジナル) /