Using caching and memoization to optimize Python performance

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Yet another nicely put video!!!

BTW, you mentioned you don’t understand the use of “lru” (least recently used) in the specific context you used it. I would like to clarify why it does make sense ;)..

The “lru” indicates the caching-mechanism’s replacement/evacuation policy, meaning, in case you do limit the cache’s size (with maxsize), the “algorithm” that will be used to chose which of the already-cached maxsize calls/return-values to replace with the next non-cached call/return-value. In the “lru” case, the item that will be replaced is the one that was least-recently used, hence, this algorithm’s best performance will be when the most recent calls are really the best predictors for your next/future calls.

I hope that helps a bit ;)!

eyalfrish
Автор

Hi Sebastiaan, this video was great. I really appreciate that you chose non-trivial functions to illustrate this.

cjayhorton
Автор

Awesome explanation. Please make more such short and concise videos.

btcjj
Автор

Great explaination as always, thanks Sebastiaan!

michakwiatek
Автор

this needs way more views. thanks allot for this

zeroxd.cypher
Автор

Really great work. Your explanations are awesome! Thank you 👍

hahahihi
Автор

Nice video, I have a few questions about the decompose function shown in the video:
1. Does one need to use `decompose(y)` (on line 18) couldn't one just write `[y]` since the first valid y found will be a prime number anyway or did you apply decompose again so that the `[y]` would be cached? If so would that really be faster than just `[y]`? Wouldn't the readability of `[y] + decompose(x // y)` be better?
2. Also shouldn't `x <= 1 or not isinstance(x, int)` be written as `not isinstance(x, int) or x <= 1` because if a given x isn't a numeric type it might throw a TypeError before the type check is done in the following statement. Would that make the code better?
3. Furthermore wouldn't it be better to use `x % y == 0` instead of `not x % y` since it seems to me that it would be more readable. Or maybe there is a reason you chose to use `not...` instead?
4. Wouldn't it also be more efficient if you iterated through numbers in `range(2, ceil(sqrt(x))+1)` since his would reduce the number of tried numbers substantially.
5. I know that demonstrating caching was the point of the video but is factorizing integers really the fastest way of checking if a certain integer is a prime number? If not what would be the fastest?
I know these questions may seem like small details but I'm still learning python and would love to improve my coding style. Would love to hear what you think about these points and if they are actually valid or if there was something I forgot? Thanks for the tutorials I hope you make more in the future.

Philogy
Автор

I don't understand one think at 1:21 when you spoke for an exemple about the square root fonction, you said that if we call that fonctions square root(9)twice we will get 3, but why we call the fonctions twice? Why we don't use it for one time?

sishuiutchiwa
Автор

i believe is dynamic programming using recursive functions called memoization

mehdi-vlnn
Автор

I don't understand how the decompositon func returns a list, it just returns a decomp of y + decomp of remainder....how does that return a list without a list being created in the function to store the values for each loop?

cryptobitez
Автор

Why does random_prime also increase in speed when it's not decorated?

Tntpker
Автор

A function that calls itself 😢.. I gotta learn more about

larrysummer