Last active
October 25, 2021 12:31
-
-
Save kthy/4714903ba229064aa198a3f679e336b3 to your computer and use it in GitHub Desktop.
Python: Least-Recently-Used Cache Demonstration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Least-Recently-Used Cache Demonstration.""" | |
from functools import lru_cache | |
@lru_cache(maxsize=5) | |
def powpow(input: int) -> int: | |
output = input**2 | |
print(f"Computed: {output:3}", end=", ") | |
return output | |
if __name__ == "__main__": | |
for i in range(1, 11): | |
for j in range(0, 3): | |
print(f"(i, j)=({i:2},{j:2})", end=" | ") | |
print(f"Cached: {powpow(i):5}") | |
for n in range(10, 0, -1): | |
for p in range(0, 3): | |
print(f"(n, p)=({n:2},{p:2})", end=" | ") | |
print(f"Cached: {powpow(n):5}") | |
print(powpow.cache_info()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
❯ python .\lru_demo.py | |
(i, j)=( 1, 0) | Computed: 1, Cached: 1 | |
(i, j)=( 1, 1) | Cached: 1 | |
(i, j)=( 1, 2) | Cached: 1 | |
(i, j)=( 2, 0) | Computed: 4, Cached: 4 | |
(i, j)=( 2, 1) | Cached: 4 | |
(i, j)=( 2, 2) | Cached: 4 | |
(i, j)=( 3, 0) | Computed: 9, Cached: 9 | |
(i, j)=( 3, 1) | Cached: 9 | |
(i, j)=( 3, 2) | Cached: 9 | |
(i, j)=( 4, 0) | Computed: 16, Cached: 16 | |
(i, j)=( 4, 1) | Cached: 16 | |
(i, j)=( 4, 2) | Cached: 16 | |
(i, j)=( 5, 0) | Computed: 25, Cached: 25 | |
(i, j)=( 5, 1) | Cached: 25 | |
(i, j)=( 5, 2) | Cached: 25 | |
(i, j)=( 6, 0) | Computed: 36, Cached: 36 | |
(i, j)=( 6, 1) | Cached: 36 | |
(i, j)=( 6, 2) | Cached: 36 | |
(i, j)=( 7, 0) | Computed: 49, Cached: 49 | |
(i, j)=( 7, 1) | Cached: 49 | |
(i, j)=( 7, 2) | Cached: 49 | |
(i, j)=( 8, 0) | Computed: 64, Cached: 64 | |
(i, j)=( 8, 1) | Cached: 64 | |
(i, j)=( 8, 2) | Cached: 64 | |
(i, j)=( 9, 0) | Computed: 81, Cached: 81 | |
(i, j)=( 9, 1) | Cached: 81 | |
(i, j)=( 9, 2) | Cached: 81 | |
(i, j)=(10, 0) | Computed: 100, Cached: 100 | |
(i, j)=(10, 1) | Cached: 100 | |
(i, j)=(10, 2) | Cached: 100 | |
(n, p)=(10, 0) | Cached: 100 | |
(n, p)=(10, 1) | Cached: 100 | |
(n, p)=(10, 2) | Cached: 100 | |
(n, p)=( 9, 0) | Cached: 81 | |
(n, p)=( 9, 1) | Cached: 81 | |
(n, p)=( 9, 2) | Cached: 81 | |
(n, p)=( 8, 0) | Cached: 64 | |
(n, p)=( 8, 1) | Cached: 64 | |
(n, p)=( 8, 2) | Cached: 64 | |
(n, p)=( 7, 0) | Cached: 49 | |
(n, p)=( 7, 1) | Cached: 49 | |
(n, p)=( 7, 2) | Cached: 49 | |
(n, p)=( 6, 0) | Cached: 36 | |
(n, p)=( 6, 1) | Cached: 36 | |
(n, p)=( 6, 2) | Cached: 36 | |
(n, p)=( 5, 0) | Computed: 25, Cached: 25 | |
(n, p)=( 5, 1) | Cached: 25 | |
(n, p)=( 5, 2) | Cached: 25 | |
(n, p)=( 4, 0) | Computed: 16, Cached: 16 | |
(n, p)=( 4, 1) | Cached: 16 | |
(n, p)=( 4, 2) | Cached: 16 | |
(n, p)=( 3, 0) | Computed: 9, Cached: 9 | |
(n, p)=( 3, 1) | Cached: 9 | |
(n, p)=( 3, 2) | Cached: 9 | |
(n, p)=( 2, 0) | Computed: 4, Cached: 4 | |
(n, p)=( 2, 1) | Cached: 4 | |
(n, p)=( 2, 2) | Cached: 4 | |
(n, p)=( 1, 0) | Computed: 1, Cached: 1 | |
(n, p)=( 1, 1) | Cached: 1 | |
(n, p)=( 1, 2) | Cached: 1 | |
CacheInfo(hits=45, misses=15, maxsize=5, currsize=5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment