项目作者: chr5tphr

项目描述 :
Minimally cache python function outputs on disk!
高级语言: Python
项目地址: git://github.com/chr5tphr/funcache.git
创建时间: 2019-09-19T12:40:37Z
项目社区:https://github.com/chr5tphr/funcache

开源协议:

下载


Funcache

Caching function outputs by hashing inputs using MetroHash.

Example

  1. from timeit import timeit
  2. from shutil import rmtree
  3. import numpy as np
  4. from funcache import cache
  5. cache_path = '/tmp/__democache__'
  6. # default path to store cache can be set like the following:
  7. @cache(cache_path)
  8. def linear(data, weight, bias):
  9. return data @ weight + bias
  10. for size in [1024, 2048, 4096, 8192]:
  11. data = np.zeros((size, size))
  12. weight = np.zeros((size, size))
  13. bias = np.zeros((size,))
  14. # the original, uncached function can be accessed as linear.__wrapped__
  15. dtime_u = timeit(
  16. lambda: linear.__wrapped__(data, weight, bias),
  17. number=1
  18. )
  19. # caching path can also be set using the keyword argument '_cache',
  20. # and disabled by setting it to None
  21. dtime_1 = timeit(
  22. lambda: linear(data, weight, bias, _cache=cache_path),
  23. number=1
  24. )
  25. dtime_2 = timeit(lambda: linear(data, weight, bias), number=1)
  26. print("Size: {}".format(size))
  27. print(" Uncached: {:.3f}s".format(dtime_u))
  28. print(" Cached 1: {:.3f}s".format(dtime_1))
  29. print(" Cached 2: {:.3f}s".format(dtime_2))
  30. rmtree(cache_path)