Python Memory Management and Tips Transcripts
Chapter: Efficient data structures
Lecture: Container sizes, lists
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Well, we actually already have the data loaded.
0:02
We have ages. We have names.
0:04
And if you want the name and age of,
0:07
say, the fourth person, that would be ages of 3,
0:11
and name or names like that. In a sense were storing it.
0:17
Let's ask, "how much memory are we using here?"
0:22
So let's have a name_size.
0:25
We're gonna actually compute how much the size is first, and then gonna put that
0:30
in the printout because I want to get the total size.
0:31
These are sort of two things that we're going to keep track of.
0:34
So let's say "print, storing them just as lists".
0:42
This would be our size_util, and we'll say "get.full.size.of", remember the
0:47
sys.getobjectsize is not sufficient,
0:49
especially for containers. We'll have ages,
0:52
Ah, I said name didn't I? Names.
0:55
And then, let's do up here age_size,
0:57
this will be "ages", and then "total", like this.
1:09
And let's just print it out. Alright,
1:11
we're gonna convert that to kilobytes and not show decimals on the end,
1:14
so it's easier to think about. And, yeah,
1:17
put a little tab so they hopefully lineup the same here.
1:20
Names, name_size, and we got the total right there.
1:29
Alright, well, let's give it a run and see what we've got.
1:33
Well, there they are. You know,
1:34
it doesn't necessarily mean a lot at this point because we don't have any alternative to
1:39
compare them to you. We don't want to say "well,
1:40
lists are more, or they're less than this other way of storing them"
1:44
What we find for storing 100,000 people,
1:46
their names and their ages is gonna be about 1.6, 1.7 MB using just straight lists.