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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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_SCOPEso 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 92repeatedly 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