Skip to content

Caching

Whenever some high-level task should be executed over and over again (for example, during a parameter optimization), it's recommended to occasionally clear the cache with clear_cache and collect the memory garbage to avoid growing RAM consumption through cached and dead objects.

Clear cache and collect garbage once every 1000 iterations
for i in range(1_000_000):
    ...  # (1)!

    if i != 0 and i % 1000 == 0:
        vbt.flush()  # (2)!
  1. Place your code here
  2. Same as calling vbt.clear_cache() and vbt.collect_garbage()

+

To clear the cache of some particular class, method, or instance, pass it directly to the function.

Clear the cache associated with various objects
vbt.clear_cache(vbt.PF)  # (1)!
vbt.clear_cache(vbt.PF.total_return)  # (2)!
vbt.clear_cache(pf)  # (3)!
  1. Applied to all instances and attributes of a particular class
  2. Applied to a particular attribute of all instances of a particular class
  3. Applied to all attributes of a particular class instance

+

To print various statistics on the currently stored cache, use print_cache_stats.

Various way to print cache statistics
vbt.print_cache_stats()  # (1)!
vbt.print_cache_stats(vbt.PF)  # (2)!
  1. Globally
  2. For some object only

+

To disable or enable caching globally, use disable_caching and enable_caching respectively.

Disable caching globally
vbt.disable_caching()

+

To disable or enable caching within a code block, use the context managers CachingDisabled and CachingEnabled respectively.

How to disable caching within a code block
with vbt.CachingDisabled():  # (1)!
    ...  # (2)!

...  # (3)!

with vbt.CachingDisabled(vbt.PF):  # (4)!
    ...
  1. Globally
  2. Place your code that doesn't require caching here
  3. Place your code that requires usual behavior here
  4. For some object only