Python Memory Management and Tips Transcripts
Chapter: Efficient data structures
Lecture: Container sizes, starter data
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's explore this idea of container sizes. So we'll come over here and make a new Python file "app_container_sizes_compared". Let's go crazy.
0:10
Make it long. Do our fMain magic on it. Go ahead and set it to run when I hit the hotkey, and there we go.
0:16
Okay, so we're gonna do a couple of things. First, I want to have the day to feel somewhat real. So I've got some fake names. Now,
0:25
we can create fake ages easily, but I want to create some names that are, like, generally the size of real names.
0:31
And there's enough variability and stuff. So I've copied this thing called mock names and check it out.
0:37
It's a bunch of names. It's like 1000 different names. What we're gonna do is we're actually gonna write a tiny bit of code that's going
0:44
to take, find all the first names, all the last names and then randomly create other names out of them.
0:50
Okay, that should be easy enough to do. I also want to use that size utility over here. Remember, we started out with this
0:58
size_util right there? To do cool stuff? Well, in order to use that I need to come over and write something like this "import size_util".
1:10
No problem. It seems fine. It's fine because I'm running PyCharm and PyCharm knows all of these blue directories,
1:16
these marked as source roots are candidates for looking for top level things.
1:22
But if you happen to use another editor or you just have this directory here, it's going to fail. So let me give you a little bit of help here.
1:29
We're gonna say, we're gonna go back, up a folder, and then add this folder to the root. So I'll just import these things that we need here.
1:41
So this will allow us you size_util, regardless of how you run it, right? I'm running PyCharm so I don't really need to do this,
1:48
But just to make life a little bit easier, we're gonna do this, alright? To make it less error prone. Next down here, we're gonna go to random, random.
1:58
Obviously, this is something going to do a lot with, create a bunch of random things, and we're going to seed it to 42 or pick a random,
2:06
just some number. This means it's always going to start from the same place and it'll basically be deterministic in what it generates for us.
2:14
So that way we always get exactly the same output in terms of size and name- length and whatnot. Then we're gonna have a count of how many times or
2:22
how many names and ages we want to work with, and we're gonna work with 100,000 details around people. And then I'm gonna need the ages.
2:32
So I'm gonna have a function that says "generate_ages" and it'll pass the count. Now this, this one's pretty easy. We want it to generate the ages.
2:44
How are we gonna do that? Well, we're just going to return a little list comprehension here.
2:48
We're going to say "return random.randint between 18 and 100, for nothing in range from zero to count".
3:01
That's it. And let's just print out ages really quick. Print.. Maybe not all of them, let's print the top 10 with an "s" There,
3:09
there we go. Those look like realistic ages, Right? Okay, so we're generating the ages and let's do the same thing for
3:16
names. Let's be explicit. This is a list of "int", and this is going to be a list of "str", and we'll write "names".
3:30
Now, generate_names is a little bit more complicated. It's not really that hard, but it's also entirely not the point of what we're
3:36
doing. So I'm gonna just drop this in here and you can see what we're gonna get is, I wrapped that around
3:42
so it fits a little better. So what we're gonna do is we're gonna go read this and go line by line, pull
3:48
this apart, and we're just gonna give back the name. And then for each one of these were going to randomly select one of these out
3:57
of the full names. I guess we're not breaking it apart. We're just gonna reuse some of the names, but there's 1000 of them, so that should be enough.
4:04
Okay, this really could be just a straight comprehension, but that's fine. We'll just leave it like it is. And let's do the same thing
4:11
and print out "names" just to see that we're getting some. Yeah, Katheryn Orknay and so on, they all look decent to me.
4:21
All right, well, now things are set up, everything started. What we have to do is test the container sizes, use various containers.
4:31
So we're going to go and use this "store_ages" in different ways, potentially store names in different ways and so on, and then when I ask the question
4:41
"well, how much memory does that take, for storing them in classes or for storing them in data frames and so on?"