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)!
- Place your code here
- Same as calling
vbt.clear_cache()andvbt.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)!
- Applied to all instances and attributes of a particular class
- Applied to a particular attribute of all instances of a particular class
- 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)!
- Globally
- For some object only
+
To disable or enable caching globally, use disable_caching and enable_caching respectively.
+
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)!
...
- Globally
- Place your code that doesn't require caching here
- Place your code that requires usual behavior here
- For some object only