Advertisement
gubichas

6

Dec 20th, 2022
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.90 KB | None | 0 0
  1. #6
  2. import collections
  3. import functools
  4. import time
  5. from collections import Counter
  6.  
  7. from heapq import nsmallest
  8. from operator import itemgetter
  9. def lru_cache(n,maxsize=100):
  10.     if n ==1:
  11.  
  12.         def decorating_function(user_function):
  13.             cache = collections.OrderedDict()    # order: least recent to most recent
  14.  
  15.             @functools.wraps(user_function)
  16.             def wrapper(*args, **kwds):
  17.                 key = args
  18.                 if kwds:
  19.                     key += tuple(sorted(kwds.items()))
  20.                 try:
  21.                     result = cache.pop(key)
  22.                     wrapper.hits += 1
  23.                 except KeyError:
  24.                     result = user_function(*args, **kwds)
  25.                     wrapper.misses += 1
  26.                     if len(cache) >= maxsize:
  27.                         cache.popitem(0)    # purge least recently used cache entry
  28.                 cache[key] = result         # record recent use of this key
  29.                 return result
  30.             wrapper.hits = wrapper.misses = 0
  31.             return wrapper
  32.         return decorating_function
  33.     else:
  34.         def decorating_function(user_function):
  35.             cache = {}  # mapping of args to results
  36.             use_count = Counter()  # times each key has been accessed
  37.             kwarg_mark = object()  # separate positional and keyword args
  38.  
  39.             @functools.wraps(user_function)
  40.             def wrapper(*args, **kwargs):
  41.                 key = args
  42.                 if kwargs:
  43.                     key += (kwarg_mark,) + tuple(sorted(kwargs.items()))
  44.  
  45.                 # get cache entry or compute if not found
  46.                 try:
  47.                     result = cache[key]
  48.                     use_count[key] += 1
  49.                     wrapper.hits += 1
  50.                 except KeyError:
  51.                     # need to add something to the cache, make room if necessary
  52.                     if len(cache) == maxsize:
  53.                         for k, _ in nsmallest(maxsize // 10 or 1,
  54.                                               use_count.iteritems(),
  55.                                               key=itemgetter(1)):
  56.                             del cache[k], use_count[k]
  57.                     cache[key] = user_function(*args, **kwargs)
  58.                     result = cache[key]
  59.                     use_count[key] += 1
  60.                     wrapper.misses += 1
  61.                 return result
  62.  
  63.             def clear():
  64.                 cache.clear()
  65.                 use_count.clear()
  66.                 wrapper.hits = wrapper.misses = 0
  67.  
  68.             wrapper.hits = wrapper.misses = 0
  69.             wrapper.clear = clear
  70.             wrapper.cache = cache
  71.             return wrapper
  72.  
  73.         return decorating_function
  74. @lru_cache(n=2,maxsize=3)
  75. def fibonacci(n):
  76.     time.sleep(0.5)
  77.     if n < 2: return n
  78.  
  79.     return fibonacci(n - 1) + fibonacci(n - 2)
  80.  
  81. print(fibonacci(50))
  82. print(fibonacci(50))
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement