Placeholder Image

Subtitles section Play video

  • Hello, everyone.

  • In today's video, I'm gonna be talking about memorization, which is one of the most important topics when it comes to making performance Web applications.

  • Then at the end of this video, we're gonna show you how dynamic programming uses memorization to make its programs more efficient.

  • Let's get started now.

  • Welcome back to Webb has simplified my name's Kyle, and my job is to simplify the Web for you so you can start building your dream project sooner.

  • So if that sounds interesting, make sure to subscribe to my channel for more videos just like this one.

  • Now to get started over here on the left hand side, I have a really simple function which just squares the number that you put into it.

  • But it's really inefficient.

  • It's purposely made to be inefficient, so we can just along this out we can say, for example, we want to get the square here of the number five, and if we save you see, we get 25 being printed out over here, and it ran pretty quickly.

  • But what happens when we want to get the square of a pretty large number?

  • If we say that you see that it takes quite a while to run.

  • If I say that again, you can see it just chugs a little bit over here.

  • It takes maybe half a second to a second to execute, which is not really ideal, especially if you need to call this a bunch of times in a row.

  • As you can see, it's very slowly executing each one, and this is definitely not what you want.

  • This is where memorization comes in.

  • The idea of memorization is that you store the previous results.

  • So that way, if we call this function a bunch of times in a row with the same value, it's going to return the other one's instantaneously.

  • So essentially, we're cashing the value based on the inputs.

  • So in order to do that on we need to do is create a variable here, and this is going to be for our previous values.

  • And we're gonna set this to an empty A rate to start.

  • Then what we want to do is after we've calculated our result after we've done all of the really hard work, we just want to store that value instead of our previous values, just like this so we can say result.

  • So essentially every single time we call this function, we're going to store the result in the position of that array.

  • And now, if I save this isn't gonna be any quicker.

  • It's still gonna be incredibly slow.

  • But this is where you can come up here and we can say if our previous values of N is already there.

  • So if it's not equal to know, then all we need to do is just return.

  • That previous value of end is a saying If we've already called this function with this variable end just returned the results since we saved it down here.

  • Now, if I run this, you could see the 1st 1 execute slowly.

  • But every single other one finishes instantaneously because all it does is look up the value that already exist.

  • Memorization is used literally everywhere, inside of programming, and some really popular use cases are this where you have a really slow function that takes a long time to execute, and you want to get the same value over and over.

  • Another really popular use case is when you're fetching.

  • External resource is so you're using the fetch a P I.

  • Then using memorization will save you from having to make a bunch of calls to the server.

  • Because you already know if you've made that call and you already know what the results are, you've essentially cashed them.

  • The last major use case of memorization is in dynamic programming and dynamic programming is when you take a recursive function that calls itself multiple times with same inputs and you actually meme wise those inputs.

  • So if you're not familiar with recursive functions, I have an entire video on them.

  • Link in the cards and the description below you can check out, but to get started.

  • Let's just remove all this code for now and imagine the Fibonacci sequence.

  • If you know what the FIBONACCI sequence is.

  • Essentially, it just takes the previous two numbers, add them together, and that gives you the new number in the sequence.

  • So a credit function called fib, which takes an end, which is the end of the number of the Fibonacci sequence we want to get and with the Fibonacci sequence, if end is either equal to one or two.

  • So essentially we're gonna save.

  • It's less than or equal to two.

  • Then we always are going to return one, because we need somewhere to start our sequence.

  • Otherwise, if it's not, we can just say here else.

  • Then we can return The Fibonacci sequence of the previous number just like this, added to the Fibonacci of the second most previous number.

  • So n minus two.

  • And what we can do is we could just call this function and log it out so we can say counsel that log fib of four.

  • And as you can see, we're getting three printed out because the first numbers one second numbers.

  • 1/3 number's, too, and the fourth number is 32 plus one.

  • And this will work for all different numbers, for example, six.

  • But as we start to increase the number, for example, if we go all the way up to 40 this is going to take quite a while.

  • As you can see, it took maybe about 1/2 a second to run, and as we get even more, let's just say 41.

  • You can see it's taking even longer.

  • That's close to double as long as 40 took, so just get slower and slower and slower really quickly.

  • But that's where memorization comes in.

  • You see, with the Fibonacci sequence, when we are calculating, these numbers were re calculating a bunch of the same things over and over again.

  • For example, just to get the Fibonacci of number five, for example, we need to calculate the Fibonacci sequence of four right here and three right here.

  • And then, in order to calculate for we need to get the Fibonacci sequence of three and two.

  • So right there, we've already repeated getting three twice, and we also have to repeat 23 times and so on.

  • So we're repeating a bunch of these Fibonacci calls, which is where we can do memorization so we can just come in here and we can say that we want to calculate our previous values and will default this to an empty ray so we can actually pass these previous values into our other Fibonacci sequence is saying, Hey, we've already calculated this.

  • You don't need to re calculate it, and then we just do a very similar thing to what we did with that square function.

  • So the first thing I want to do is we're gonna create a variable Ryner called us our results just like that.

  • Give it about equal.

  • And then here we just want to set our result equal toe one if it is less than or equal to two, and here we're gonna set a result equal to the fifth and minus one positive of n minus two.

  • Now, what we can do is we can actually take our previous values of N.

  • We can actually set this here equal to our results, just like we did in the previous example.

  • And then lastly, we just want to return our results at the bottom here.

  • And then all we need to do is we just check for a previous value.

  • So we say if previous values of N is not equal to know, then we can just return the previous values of end just like that.

  • Now, all that's left to do is to make sure we passed this previous value array down toe.

  • All of our other calls of this Fibonacci function just like that.

  • And now they helped have access to the previous values from all of the other calls.

  • And if we say that you see, this is still working over here.

  • But if we change this to be a large number?

  • For example, 40.

  • You still see that executes almost instantly.

  • What about 100 again?

  • It executes almost instantly and with the other example, without memorization and without dynamic programming, it would have taken an incredibly long time to execute Fibonacci of 100.

  • And we can even go further.

  • For example, let's say we wanted to do 500.

  • You can see that still is incredibly quick, especially compared to the old method.

  • And that's all there is to memorization and dynamic programming.

  • If you enjoyed the video, make sure to check out my other videos linked over here and subscribe to my channel for more videos just like this one.

  • Thank you very much for watching and have a good day.

Hello, everyone.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it