Och, I can't see you.
But I know what you're feeling.
You're doubting me right now.
You're thinking generators are useless.
You keep saying that they're fast and performant, but I don't really get it.
This L keyword and this next.
And honestly, it just.
It just doesn't make sense.
Let me convince you of their true power.
I'm going to select all of these and just delete them, because I want to go back to an example that
we've used before.
Remember when we created the decorator performance?
In our decorated section.
Using this performance decorator.
Let's minimize it.
I have to.
Functions for us a long time function that uses a range.
And another longtime function that uses a range and then converts it into a list.
So remember, one creates a list.
And then from that list that's created in memory on our computers, it's going to go one by one and
multiply things by five versus a range, which is a generator that comes built into Python that is going
to just one by one hold zero and memory and multiply by five, hold one memory, but multiply by five
and keeps going, keeps going, keeps going and removes from memory any old numbers.
What do you think the performance of both of these will be?
Let's test it out.
And by the way, I'll link to the exercise so you can test it out yourself and play around.
So let's do long time.
And then long time to.
Place your bets.
Get ready.
Let's find out.
The first function long time took 1.6 seconds.
And then the second function took almost twice as long.
That's a pretty big difference, right?
But right away we can tell.
All right.
This is definitely more performant than well, long time, too.
Let's run it again just to test our assumptions.
Make sure it's correct.
This time it took 1.8 and then 2.6.
All right.
This is so much faster, even though they both do the same thing.
And think of this.
If you were Google, think about how much data Google has.
If you're working at Google and imagine if this was even bigger, if this was even longer, if you can
perform a task that much faster.
Use up a lot less resources than what Google has.
I mean, they would love you because with generators, we're able to not hold things in memory, not
have to consume all that resources and instead process data efficiently.
And look over here.
I was able to solve this function in 16 seconds.
I mean, yeah, that took a long time, but long time to actually could even make it it killed the process.
If this was a Google server, that's a server that just died and is completely useless.
So generators are really, really useful when calculating large sets of data, particularly if we're
using long loops where we don't really want to store that memory and we don't need to calculate everything
at the same time, maybe one by one.
And a lot of libraries in Python underneath the hood use generators instead of using lists because they're
so much faster.
So remember to create a generator.
All we have to do is create a generator function that we want.
And then in here, we can just simply loop over some value.
That perhaps we give it to a range or we can just accept us parameter.
And all we need to do is simply.
Yield.
That result.
And do whatever we want in this block.
But make sure we pause the function so that later on it can be used.
As a generator function so we can do something like a for loop and say item in gen function and give
it some sort of parameter.