Could you explain mi this: if ef_cache changes because result changes then how can old num be recognized? Is ef_cache storing those results without overrunning one with the nex one?
Summary: Memoization refers to the mechanism that makes a function not having to compute the output using an input, when the function has been executed using the same input before. This is possible because the computer memorizes the input:output pair. This tool can be demonstrated in python using an if statement and a dictionary outside the function.
Dude you're literally god, I just finished your first class functions, closure, and decorators videos and while watching this I realized how useful it would be to implement memoization using a decorator.
another brilliant video :) love learning about programming terms and such... if you could do an advanced playlist of the terms maybe in more detail/other ones would be great :) love learning concepts which like you say are not syntax/prog_lang specific.. act learning the blueprints so to speak of how and why it works
# You can turn the process into a decorator so you can add it to any function def memoization(original_function): cache = {} def inner_function(arg): if arg in cache: print(f"Result was retrieved from cache.") return cache[arg] else: print(f"Memoizing result of function {original_function.__name__}({arg}).") cache[arg] = original_function(arg) return cache[arg] return inner_function @memoization def square(number): return number ** 2
But this only seems useful when you've multiple calls of the same function with the same arguments. My point is, what could this possibly be for in a program? In many applications a function is called as many times as the program itself is called. But the cache gets cleared anyway. Could this method be used here?
this comment is old but ill add my touch, this exemple is the best by mcoding and you will maybe appreciate the decorator trick, in recursive call it can get useful ua-cam.com/video/DnKxKFXB4NQ/v-deo.html
Like your videos on Python ... Quick query on this video: Im a beginner so a very simple query though: How did you get the time of the overall execution of the program, i mean 4.0s or 2.0s in this video? Any inputs on how to get it in 'pycharm' will be helpful.
Because if it runs the code in the "if" statement then it will just immediately return... so you don't need the else statement because the code after the "if" won't be run anyways if the conditional is met. Hope that makes sense.
it is actually Corey's style. you could use different style and use else statement to get the same result. in the if block: just assign the result to result variable. if num in ef_cache: result = ef_cache[num] else: print('') time.sleep() ef_cache[num] = num*num result = num*num return result
Shouldn't the block after the if in the expensive function still execute after the if executed, even if there is a value put in that matches what's in dictionary?
Nice video Corey, thx for teaching us all this info. For all of you that need and example of the memoization used, you can reffer to the Socratica video on fibonacci sequence: ua-cam.com/video/Qk0zUZW-U_M/v-deo.html , with those two videos you will be able to understand better the concept.
Maybe your video is good for a first grasp of the term, and you could say thats the point, but memoization is also a technique that doesn't need an external data structure to work, i sugest you to read and maybe talk about it, cause nobody wants dirty globals or spaghetti oriented programming, when You can implement a memoized pure function.
My lecturer spent 2 hours trying to explain this to us didn't understand a thing. You done it in 5 and it makes total sense. THANKS!!
Could you explain mi this:
if ef_cache changes because result changes then how can old num be recognized? Is ef_cache storing those results without overrunning one with the nex one?
@@TrumanBurbonk yes ef_cache is storing those results without overrunning with the next one.
Every time I hear "Memoization" I imagine someone saying "Memorization" in a baby voice.
I'm never going to look at this the same way again
Awesome. Short, simple, and straight to the point. Thank you.
Yeah, Wikipedia is great
Summary:
Memoization refers to the mechanism that makes a function not having to compute the output using an input, when the function has been executed using the same input before. This is possible because the computer memorizes the input:output pair.
This tool can be demonstrated in python using an if statement and a dictionary outside the function.
Nice
Whenever I don't understand something and I find a Corey Schafer video I'm always relieved. I know I'm finally going to understand it.
Dude you're literally god, I just finished your first class functions, closure, and decorators videos and while watching this I realized how useful it would be to implement memoization using a decorator.
nice thought, it would really show how decorators could be robust and useful
been rockin with you for a while now, good content mr chaffer!
All your videos are well organized and delivered....thanks so much for sharing!!!!
This is the proper video for memoization explained for Python while most use the Fibonacci example
Very clear and easy to understand! Thank you for sharing.
another brilliant video :) love learning about programming terms and such... if you could do an advanced playlist of the terms maybe in more detail/other ones would be great :) love learning concepts which like you say are not syntax/prog_lang specific.. act learning the blueprints so to speak of how and why it works
Holy Moly! Thanks for that, bro. Really helps me optimized my program!
At last a clear cut explanation.
A very beautiful playlist ❤️ do more of these please. They're so useful
Hi Corey
I become your fan!
i never seen anyone teach with such clearity on youtube😊👍
Yes, he does it very well! But checkout FunFunFunction channel as well if you are in to JavaScript ofc :)
best explanation !
You are a great teacher!!
This is an awesome explanation. thank you so much!
Great work Corey!
Simplified explanation. Thanks
Clear explanation! Thanks.
Awesome explanation!!
I love you man, your videos are just so useful and easy to understand. Thanks
very clear and clean explanation, thanks!!!
Very well explained. Thank you!
Thanks. Easy to understand
Thank you Corey
Excellent video
Amazing tutorials.
Huge fan of your videos Corey!
The functools.lru_cache decorator is also a great way to implement memoization on functions.
You're videos are great! Thank you!
your videos are too good.
When I have a doubt about anything in this world, i go to youtube and write "corey schafer +anything". If not found, i quit of learning it.
such smooth explanation corey \/
Good as always. Thanks.
Thank you for your work, perfectly explain!
Such a fancy name for a simple idea.
Thank you for this nice explanation.
This is brilliant
Can you do a video on recursion ?
Great presentation. Is it okay to use "list" instead of "dictionary" in the ef_cache? Thx.
Thanks for the great video
# You can turn the process into a decorator so you can add it to any function
def memoization(original_function):
cache = {}
def inner_function(arg):
if arg in cache:
print(f"Result was retrieved from cache.")
return cache[arg]
else:
print(f"Memoizing result of function {original_function.__name__}({arg}).")
cache[arg] = original_function(arg)
return cache[arg]
return inner_function
@memoization
def square(number):
return number ** 2
But this only seems useful when you've multiple calls of the same function with the same arguments. My point is, what could this possibly be for in a program?
In many applications a function is called as many times as the program itself is called. But the cache gets cleared anyway. Could this method be used here?
I'm sure it has uses outside the classroom... somewhere
this comment is old but ill add my touch, this exemple is the best by mcoding and you will maybe appreciate the decorator trick, in recursive call it can get useful ua-cam.com/video/DnKxKFXB4NQ/v-deo.html
How are you using “print” without () ?
That program was written in Python 2.X version not in Python 3
Hope you can come up with tutorials on data structures or at the least, recursion. Please help a brother out!
All my life, I had been calling it caching ..
Like your videos on Python ...
Quick query on this video:
Im a beginner so a very simple query though:
How did you get the time of the overall execution of the program, i mean 4.0s or 2.0s in this video? Any inputs on how to get it in 'pycharm' will be helpful.
Hello, are you able to do it now (5 years)?
How do you show the time spent [Finished in _s]? Is it some configuration?
It will show you automatically In the sublime text editor
how to show that command says [Finished in 4.0s], i did not find it in vsCode, appreciate your help.
Why don't we need an else statement after the if statement in expensive_func?
Because if it runs the code in the "if" statement then it will just immediately return... so you don't need the else statement because the code after the "if" won't be run anyways if the conditional is met. Hope that makes sense.
then why do we need else statements. if the program meets the conditional it breaks after the return, if not it runs the rest of the code
it is actually Corey's style. you could use different style and use else statement to get the same result. in the if block: just assign the result to result variable.
if num in ef_cache:
result = ef_cache[num]
else:
print('')
time.sleep()
ef_cache[num] = num*num
result = num*num
return result
Shouldn't the block after the if in the expensive function still execute after the if executed, even if there is a value put in that matches what's in dictionary?
The result is returned in the if block
Will the caching still work if you import the function into a separate program?
Yes, that should still work.
well done
what is the use of sleep in this program
Sleep just pauses the execution for a bit so that we can simulate the time it might take to compute a more intensive function
thank you thank you thank you
i thought he would explain about lru_cache
amazing!!!!
If you're looking to make an intermediate series of videos, memoization using Python 3's functools.lru_cache would be pretty cool.
Thanks!
Hello if you plan to use it i suggest you to check this video more up to date 👍 ua-cam.com/video/DnKxKFXB4NQ/v-deo.html
Great !
You are god!!!!, thank you so much!
Perfect!!!
Plz zoom the code
Good
Nice video Corey, thx for teaching us all this info. For all of you that need and example of the memoization used, you can reffer to the Socratica video on fibonacci sequence: ua-cam.com/video/Qk0zUZW-U_M/v-deo.html , with those two videos you will be able to understand better the concept.
Last minute assignment anyone? xd
I came hear to learn how to pronounce the word
Maybe your video is good for a first grasp of the term, and you could say thats the point, but memoization is also a technique that doesn't need an external data structure to work, i sugest you to read and maybe talk about it, cause nobody wants dirty globals or spaghetti oriented programming, when You can implement a memoized pure function.
Im just gonna save ur time..Memoization is purely caching...
As always, very nice explanation Corey, Thanks. What about using functools.cache decorator?