Python Memory Management and Tips Transcripts
Chapter: Memory and classes
Lecture: Slots are faster, not just smaller

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The final example when we're talking about classes and memory and slots and all that kind
0:04 of stuff is, I want to show you a different side of speed.
0:07 So over here in this one,
0:11 we saw that we could create a crowd quicker.
0:14 The retirement summary didn't really matter,
0:16 but we could create a crowd of 100,000 people quicker and use less memory.
0:20 But one thing you might notice about creating the crowd down here is we're not actually
0:25 accessing any of the properties. We're not like trying to read the first name or the
0:29 birthdate or any of those things, we're
0:30 just creating the objects and letting them go.
0:33 So what you'll see is that actually with slots.
0:36 there's efficiencies to be gained around
0:38 just accessing the data. So, let's look at some examples.
0:41 We're gonna go and say, Create one object,
0:44 this is our semi-improved person, it has fields,
0:48 and it has properties are we're only gonna work with the fields,
0:50 not the properties. So what we're gonna do is we're gonna create a standard one here like this
0:54 and then we're gonna say we're testing it,
0:57 and we're gonna run this function
0:58 "test_access()", and what test_access does it just goes 100,000 times and it says
1:03 "go give me the object here, give me the field, the first name,
1:09 the last name, the birthdate, basically read the fields and assign them the local
1:13 variables, throw it away, and do it 100,000 times".
1:16 And it gives this little report how long it took.
1:19 And then we're going to do with the same thing
1:20 but with slots, remember? This one will have those fields backed by dictionaries.
1:24 This one will have them backed more like by this list thing and index, here.
1:29 So we got better memory, is there a cost to be paid, or a benefit to be
1:34 gained on either side of having these slots? Because it may be lower memory,
1:38 but it takes longer to access them.
1:39 Or do we have benefits around access
1:41 speed or whatnot? What we're gonna do is we're gonna go over here and
1:46 just print this out, and say
1:47 "how much faster is it?" Maybe I'll just do one other test just to make
1:52 sure that we'll say "if standard time is less than slot time, print".
2:01 Okay, so we're gonna just do that and, you know, call this function right here,
2:05 which is going to take this one object and access its fields, all of them, 100,000
2:09 times. And just time it.
2:10 Let's go and see what we get.
2:14 Alright, look at this. Slots were faster.
2:16 Okay, so, great, slots were faster.
2:18 And for doing 400,000 operations, that is four attribute reads, 100,000 times, took 25 milliseconds
2:26 and that's 15.6 million operations per second.
2:32 But with slots, 22 milliseconds and 18.1 or 18.2 million operations per second,
2:38 that's a gain of 16%. So not only do we use less memory,
2:43 not only are they faster to allocate,
2:45 actually reading their properties, their fields, specifically, is faster.
2:49 Now there's some variability here.
2:50 Let's just run this over. So there's 3, 8, 14, 9, 13, 13 right?
2:58 I am doing screen recording right now,
2:59 which takes up a lot of its CPU cycles and whatnot.
3:02 So this is not super consistent,
3:04 so you just run it a couple times, see what you get,
3:05 but it's pretty clear there's at least a 10% advantage to be gained,
3:09 sometimes 20% advantage for these different operations.
3:13 And to me, that is just awesome.
3:16 Like I said, earlier, what did I say? Slots,
3:18 double thumbs, or triple thumbs up because less memory, faster to allocate, and faster to
3:23 work with? That seems like a huge trade off to give up that little bit
3:27 of dynamic flexibility, which usually we don't use in our code,
3:31 anyway. Slots are awesome, and here's one more reason why.