Memoizing Fibonacci with bkt #28
dimo414
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Here's a naive Fibonacci implementation:
And here's the same approach but using bkt to memoize intermediate results:
Here's a timing comparison:
The only change that is actually needed is prefixing the recursive calls with
bkt --ttl=30s --
- this caches the subproces invocations and therefore each intermediate value will only be executed once. However we also set a customBKT_SCOPE
so that each invocation is cached separately. Without this two separate invocations will share the same cache and the second call would complete ~immediately (assuming the TTL hasn't expired yet). Try runningtime BKT_SCOPE=1 /tmp/cached.sh 92
repeatedly and you'll see the cache is shared. Of course normally this is a good thing! But scopes let you isolate cache pools when desired.Beta Was this translation helpful? Give feedback.
All reactions