https://github.com/chr5tphr/funcache
Minimally cache python function outputs on disk!
https://github.com/chr5tphr/funcache
cache disk functions hash memoization metrohash numpy python pytorch
Last synced: 2 months ago
JSON representation
Minimally cache python function outputs on disk!
- Host: GitHub
- URL: https://github.com/chr5tphr/funcache
- Owner: chr5tphr
- Created: 2019-09-19T12:40:37.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-10-15T11:23:35.000Z (over 6 years ago)
- Last Synced: 2025-08-10T04:34:56.805Z (11 months ago)
- Topics: cache, disk, functions, hash, memoization, metrohash, numpy, python, pytorch
- Language: Python
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Funcache
Caching function outputs by hashing inputs using MetroHash.
## Example
```python
from timeit import timeit
from shutil import rmtree
import numpy as np
from funcache import cache
cache_path = '/tmp/__democache__'
# default path to store cache can be set like the following:
@cache(cache_path)
def linear(data, weight, bias):
return data @ weight + bias
for size in [1024, 2048, 4096, 8192]:
data = np.zeros((size, size))
weight = np.zeros((size, size))
bias = np.zeros((size,))
# the original, uncached function can be accessed as linear.__wrapped__
dtime_u = timeit(
lambda: linear.__wrapped__(data, weight, bias),
number=1
)
# caching path can also be set using the keyword argument '_cache',
# and disabled by setting it to None
dtime_1 = timeit(
lambda: linear(data, weight, bias, _cache=cache_path),
number=1
)
dtime_2 = timeit(lambda: linear(data, weight, bias), number=1)
print("Size: {}".format(size))
print(" Uncached: {:.3f}s".format(dtime_u))
print(" Cached 1: {:.3f}s".format(dtime_1))
print(" Cached 2: {:.3f}s".format(dtime_2))
rmtree(cache_path)
```