#!/usr/bin/env python # encoding=utf-8 from lru import LRUCache, LRUCacheOrdered from lfu import LFUCache import time if __name__ == '__main__': lru = LRUCache(2) lru.set(1, '1') lru.set(2, '2') lru.set(3, '3') print lru.get(1) n = 10**5 lru = LRUCache(n / 2) start = time.time() for i in xrange(n): lru.set(i, None) end = time.time() print 'total time:%.3f' % (end - start) print '%.3f / second' % (n / (end - start)) start = time.time() for i in xrange(n): lru.get(i) end = time.time() print 'total time:%.3f' % (end - start) print '%.3f / second' % (n / (end - start)) a = raw_input()
class TinyLFU: def __init__(self, size=1000000, sample=100000, false_positive=0.01): self.__age = 0 self.__sample = sample self.counter = CM4(size) self.doorkeeper = Doorkeeper(sample, false_positive) if size <= 1: size = 2 # percentage from https://arxiv.org/abs/1512.00727 lru_percent = 1 lru_size = (lru_percent * size) / 100 if lru_size < 1: lru_size = 1 self.lru = LRUCache(cache_size=lru_size) slru_size = math.ceil(((100.0 - lru_percent) / 100.0) * size) slru20_size = math.ceil(slru_size * 0.2) if slru20_size < 1: slru20_size = 1 self.slru = SLRUCache(probation_cap=slru20_size, protect_cap=slru_size - slru20_size) def __len__(self) -> int: return len(self.lru) + len(self.slru) def __contains__(self, key) -> bool: return key in self.lru or key in self.slru def get(self, key: str): # for tinylfu aging, reset only admission self.__age += 1 if self.__age == self.__sample: self.counter.reset() self.doorkeeper.reset() self.__age = 0 self.counter.add(key) value = self.lru.get(key) if value != None: # timeout return value value = self.slru.get(key) if value != None: # timeout return value def set(self, key: str, value): if key in self.slru: self.slru.remove(key) old_key, old_value, evicted = self.lru.set(key, value) if not evicted: return victim_key = self.slru.victim() if victim_key == None: self.slru.set(old_key, old_value) return if not self.doorkeeper.allow(old_key): # on evict return victim_count = self.counter.estimate(victim_key) item_count = self.counter.estimate(old_key) if victim_count < item_count: self.slru.set(old_key, old_value) else: # on evict return def remove(self, key: str) -> object: value = self.lru.remove(key) if value != None: return value value = self.slru.remove(key) if value != None: return value
f.close() f = open('names_2.txt', 'r') names_2 = f.read().split("\n") # List containing 10000 names O(n) f.close() duplicates = [] # Return the list of duplicates in this data structure # Replace the nested for loops below with your improvements all_names = names_1 + names_2 lrucache = LRUCache(20000) for name in all_names: # if the name is already in the cache add to duplicates if lrucache.get(name): duplicates.append(name) lrucache.set(name, name) # for name in names_1: # if lrucache.get(name): # duplicates.append(name) # lrucache.set(name, name) # for name in names_2: # if lrucache.get(name): # duplicates.append(name) # lrucache.set(name, name) # 4.5 seconds # dll = DoublyLinkedListMod() # for name in names_1: # # add item to dll
#!/usr/bin/env python # encoding=utf-8 from lru import LRUCache, LRUCacheOrdered from lfu import LFUCache import time if __name__ == '__main__': lru = LRUCache(2) lru.set(1, '1') lru.set(2, '2') lru.set(3, '3') print lru.get(1) n = 10**5 lru = LRUCache(n/2) start = time.time() for i in xrange(n): lru.set(i, None) end = time.time() print 'total time:%.3f' % (end - start) print '%.3f / second' % (n/(end-start)) start = time.time() for i in xrange(n): lru.get(i) end = time.time() print 'total time:%.3f' % (end - start) print '%.3f / second' % (n/(end-start)) a = raw_input()
from lru import LRUCache if __name__ == '__main__': cache_limit = 4 cache = LRUCache(cache_limit) for i in range(0, 10): cache.set(i, '{0}00'.format(i)) if i % 2 == 0: print('Get a key', cache.get(0)) print(cache.cache) # STD_OUT # Get a key 000 # OrderedDict([(0, '000')]) # OrderedDict([(0, '000'), (1, '100')]) # Get a key 000 # OrderedDict([(1, '100'), (2, '200'), (0, '000')]) # OrderedDict([(1, '100'), (2, '200'), (0, '000'), (3, '300')]) # Get a key 000 # OrderedDict([(2, '200'), (3, '300'), (4, '400'), (0, '000')]) # OrderedDict([(3, '300'), (4, '400'), (0, '000'), (5, '500')]) # Get a key 000 # OrderedDict([(4, '400'), (5, '500'), (6, '600'), (0, '000')]) # OrderedDict([(5, '500'), (6, '600'), (0, '000'), (7, '700')]) # Get a key 000 # OrderedDict([(6, '600'), (7, '700'), (8, '800'), (0, '000')]) # OrderedDict([(7, '700'), (8, '800'), (0, '000'), (9, '900')])