Python Memory Management and Tips Transcripts
Chapter: Efficient data structures
Lecture: Container sizes, arrays

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Sp lists seem like a clear winner, maybe, but remember, lists actually are super inefficient.
0:06 They've got their contiguous structure to hold the pointers, but then they point out at pieces of numbers in memory.
0:15 And remember, those numbers are like 28 bytes a piece, where storing most these numbers, they're ages, they're not very big at all,
0:23 so we should be able to do better. And if these numbers were larger,
0:26 still, remember, we're working with numbers that are within the range of that flyweight pattern. So it's probably not as big of a deal as
0:34 It could be. I'll maybe tweak that just a little bit to see what we get in terms of if it were different types of data.
0:41 But what we do is we're going to say we're "storing them as arrays". So this is not a common type used in Python, but it is an interesting one.
0:51 So we'll say "ar = array". Now, this is a type that we can import like this, and, array, and then you can give it a type. See that type code?
1:03 It can only hold one of the same things. If we want it to hold integers, we can use a "B". It can
1:09 also hold floats and stuff like that. We'll say "ar.fromlist" and we'll give it the ages, let's grab this and put that here say "array_size =
1:30 size_util.get_full_size(ar)". "array_size". Perfect. Now let's run it and look at that.
1:46 Wow. So instead of using 800 kilobytes or something way more for whatever that is it's using 100. So there you can
1:55 see, way, way better. 106 kilobytes instead of 800. So again, we're getting almost this 10x difference by choosing a slightly different
2:04 data structure. So that's pretty awesome. If you go look at the documentation for array,
2:11 you'll see that there's different types like we're using a "B", which is an unsigned character. So maximum size in bytes is 1,
2:18 so I think this is 0 to 256 probably, but if you have longer data, bigger data rather, like a float, you can say it's an "F",
2:27 or you could say it's a "signed long". But be aware that when you create one of these and you put something in there
2:33 every entry that we had before was just one byte, and that was why it was really efficient. Where as over here, if you say it's a float,
2:42 right, it's gonna take 4 times as much memory, but of course it can hold the things that go in it. So keep in mind, this is only for numbers.
2:49 We can't apply this cool trick to strings, but nonetheless we still got cool memory reduction accomplished,
2:57 right? So we could drop that one down by 700 KB at least and have names and then this type of array to store. So if you're storing a lot of numbers,
3:05 it's very likely that these arrays are going to help you a lot.


Talk Python's Mastodon Michael Kennedy's Mastodon