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