Using caching and memoization to optimize Python performance

  Рет қаралды 8,326

Sebastiaan Mathôt

Sebastiaan Mathôt

Күн бұрын

Пікірлер: 28
@cjayhorton6356
@cjayhorton6356 4 жыл бұрын
Hi Sebastiaan, this video was great. I really appreciate that you chose non-trivial functions to illustrate this.
@eyalfrish
@eyalfrish 5 жыл бұрын
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 ;)!
5 жыл бұрын
Thanks for clarifying that!
@michakwiatek2076
@michakwiatek2076 5 жыл бұрын
Great explaination as always, thanks Sebastiaan!
@btcjj955
@btcjj955 3 жыл бұрын
Awesome explanation. Please make more such short and concise videos.
@zeroxd.cypher3899
@zeroxd.cypher3899 4 жыл бұрын
this needs way more views. thanks allot for this
@benyaminyakobi3652
@benyaminyakobi3652 3 жыл бұрын
Thank you, very clear explanation!
@hahahihi6253
@hahahihi6253 5 жыл бұрын
Really great work. Your explanations are awesome! Thank you 👍
@AjitSingh-rg3zu
@AjitSingh-rg3zu 4 жыл бұрын
Thanks for the wonderful explanation
@nitinpandey7478
@nitinpandey7478 3 жыл бұрын
Nicely explained
@raspreetsingh5542
@raspreetsingh5542 5 жыл бұрын
Great stuff!
@sergeyv1534
@sergeyv1534 5 жыл бұрын
Good lesson!
@Philogy
@Philogy 5 жыл бұрын
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
5 жыл бұрын
Hi Philippe, thanks for thinking about this in so much detail! I think you're correct on each of these points! Re 5: no, there's undoubtedly faster ways to determine whether a number is prime!
@korkunctheterrible4302
@korkunctheterrible4302 2 жыл бұрын
How does it matter what range he writes when he's gonna return the function as soon as the smallest prime factor checks? It's not like he was trying to list all the prime factors that divide evenly into the input, he's instead doing a total factorization that recurses at the first smallest prime factor it encounters. For instance, if the input were 8, there'd be 4 primes up to 8 but only 1 of them would be a factor 3 times, it woudn't ever check for 3-8 anyway . So can you please tell me what I'm missing?
@mehdi-vl5nn
@mehdi-vl5nn 3 жыл бұрын
i believe is dynamic programming using recursive functions called memoization
@onlymusic2005
@onlymusic2005 4 жыл бұрын
Great performance, ❤️
@sishuiutchiwa5762
@sishuiutchiwa5762 4 жыл бұрын
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?
@himanshuchauhan1015
@himanshuchauhan1015 3 жыл бұрын
it's supposed that the function call is needed to call twice, it can be simultaneously or after some time or after some other things complete
@memyaccount8213
@memyaccount8213 10 ай бұрын
The specific reason doesn't matter; it's more about how it would be recalculating the square root in the same way each time it's called. But with memoization, it wouldn't be recalculated in the same way as the first time it was called--instead, it would use what was stored from the first call, in order to more efficiently give you the answer.
@DanielDelgado-ge7rs
@DanielDelgado-ge7rs 3 жыл бұрын
Te amo
@cryptobitez6090
@cryptobitez6090 2 жыл бұрын
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?
@korkunctheterrible4302
@korkunctheterrible4302 2 жыл бұрын
The recursion base case is when x in [x] is a prime number. so let's say we call the func on 12: dec(12) * return dec(2) + dec(6) dec(2) for y in range(2,2) means for nada times, out of the loop, return [2] - - - > our initial list , and the return statement above has now become: return [2] + dec(6) dec(6): *return dec(2) + dec(3) dec(2) returns [2] again, (probably this is what is memoized/returned from cache), so the return statement is now return [2] + [2] + dec(3) now we look at dec(3): for y in range(2,3): - - - >the only iteration is y=2 and since 3 % 2 != 0 therefore truthy, the if block is not executed yet again, and [3] is returned out of the loop the original return statement becomes [2, 2] + [3] which is [2, 2, 3 ] the plus sign can merge lists like this, I think it's "in-place", lists are mutable, although the addition is not the same as .append(), it bears the same results in such cases The trick here is that only part of the first return statement that calls decompose on a prime number exits recursion and initiates the list and only the part of the first return statement that's still a composite number continues on with the recursion, always returning dec(smallest prime factor) + dec(new composite number).
@pliniado
@pliniado Жыл бұрын
@@korkunctheterrible4302 Thank you very much! I was struggling with the same difficult. Very clear explanation.
@Tntpker
@Tntpker 4 жыл бұрын
Why does random_prime also increase in speed when it's not decorated?
@jampk24
@jampk24 4 жыл бұрын
The random_prime() function uses the decompose() function, so if you speed up decompose() then you're also speeding up random_prime().
@Tntpker
@Tntpker 4 жыл бұрын
@@jampk24 cheers
@larrysummer2015
@larrysummer2015 3 жыл бұрын
A function that calls itself 😢.. I gotta learn more about
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 857 М.
5 Python Libraries You Should Know in 2025!
22:30
Keith Galli
Рет қаралды 91 М.
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН
To Brawl AND BEYOND!
00:51
Brawl Stars
Рет қаралды 17 МЛН
Python dataclasses will save you HOURS, also featuring attrs
8:50
How is this Website so fast!?
13:39
Wes Bos
Рет қаралды 1,4 МЛН
Top 18 Most Useful Python Modules
10:50
Tech With Tim
Рет қаралды 937 М.
The Single Most Useful Decorator in Python
3:50
mCoding
Рет қаралды 381 М.
Writing Code That Runs FAST on a GPU
15:32
Low Level
Рет қаралды 583 М.
The Absolute Best Intro to Monads For Software Engineers
15:12
Studying With Alex
Рет қаралды 683 М.
When Optimisations Work, But for the Wrong Reasons
22:19
SimonDev
Рет қаралды 1,2 МЛН
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Modern Python logging
21:32
mCoding
Рет қаралды 220 М.