-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.py
More file actions
33 lines (24 loc) · 831 Bytes
/
basic.py
File metadata and controls
33 lines (24 loc) · 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# /// script
# requires-python = ">=3.10"
# dependencies = ["warp_cache"]
# ///
"""Basic caching example — memoize an expensive function."""
import logging
from warp_cache import cache
logging.basicConfig(level=logging.INFO, format="%(message)s")
log = logging.getLogger(__name__)
@cache(max_size=256)
def fibonacci(n):
if n < 2:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
if __name__ == "__main__":
# Without caching this would be exponentially slow
result = fibonacci(80)
log.info("fibonacci(80) = %s", result)
info = fibonacci.cache_info()
log.info("Cache info: %s", info)
# Clear and recompute
fibonacci.cache_clear()
log.info("Cleared cache, recomputing fibonacci(10) = %s", fibonacci(10))
log.info("After clear + recompute: %s", fibonacci.cache_info())