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 ruclips.net/video/DnKxKFXB4NQ/видео.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.
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?
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
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: ruclips.net/video/Qk0zUZW-U_M/видео.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.
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
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.
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
All your videos are well organized and delivered....thanks so much for sharing!!!!
been rockin with you for a while now, good content mr chaffer!
Very clear and easy to understand! Thank you for sharing.
Holy Moly! Thanks for that, bro. Really helps me optimized my program!
This is an awesome explanation. thank you so much!
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
This is the proper video for memoization explained for Python while most use the Fibonacci example
Simplified explanation. Thanks
A very beautiful playlist ❤️ do more of these please. They're so useful
I love you man, your videos are just so useful and easy to understand. Thanks
very clear and clean explanation, thanks!!!
At last a clear cut explanation.
Very well explained. Thank you!
Thanks. Easy to understand
Clear explanation! Thanks.
You are a great teacher!!
best explanation !
Thank you Corey
Awesome explanation!!
Huge fan of your videos Corey!
The functools.lru_cache decorator is also a great way to implement memoization on functions.
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 :)
Thank you for your work, perfectly explain!
# 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
You're videos are great! Thank you!
Amazing tutorials.
Thank you for this nice explanation.
Great work Corey!
Excellent video
such smooth explanation corey \/
your videos are too good.
Good as always. Thanks.
Can you do a video on recursion ?
Thanks for the great video
Great presentation. Is it okay to use "list" instead of "dictionary" in the ef_cache? Thx.
This is brilliant
thank you thank you thank you
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 a fancy name for a simple idea.
Hope you can come up with tutorials on data structures or at the least, recursion. Please help a brother out!
well done
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 ruclips.net/video/DnKxKFXB4NQ/видео.html
How are you using “print” without () ?
That program was written in Python 2.X version not in Python 3
Thanks!
Hello if you plan to use it i suggest you to check this video more up to date 👍 ruclips.net/video/DnKxKFXB4NQ/видео.html
how to show that command says [Finished in 4.0s], i did not find it in vsCode, appreciate your help.
If you're looking to make an intermediate series of videos, memoization using Python 3's functools.lru_cache would be pretty cool.
You are god!!!!, thank you so much!
amazing!!!!
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
i thought he would explain about lru_cache
All my life, I had been calling it caching ..
Great !
Will the caching still work if you import the function into a separate program?
Yes, that should still work.
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
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
Good
Perfect!!!
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
Plz zoom the code
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: ruclips.net/video/Qk0zUZW-U_M/видео.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.
Last minute assignment anyone? xd
I came hear to learn how to pronounce the word
Im just gonna save ur time..Memoization is purely caching...
As always, very nice explanation Corey, Thanks. What about using functools.cache decorator?