Python 3.11: A Guided Tour Through Code Transcripts
Chapter: A Grab Bag of Minor Updates
Lecture: Performance
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
We spent a lot of time talking about performance in this course.
0:05
However, there's a few more I want to throw out that didn't really justify their own section. So we're gonna talk about that here inlining opcodes.
0:13
So we have inlining upcode caching according to this issue here we have in line caching for load attr
0:21
as well as store attr and we have it for load method as well. So some of those byte code caching operations are going to be much faster.
0:29
Same thing for binary subscript and for the compare operation.
0:34
So a lot of opcode caching which is fantastic. We also have better performance around strings.
0:42
So if you're using find, rfind both of those have improved algorithms that are faster couple more here we have
0:52
speed up of the iteration over bytes and bytearray by 30%. That's great. Very very welcome there. And this one is probably the most important.
1:03
Maybe tied for the byte code one but certainly right up there is list.append and list comprehension are now
1:11
faster. We use list of apennd and comprehension all the time. The way lists work is as you add a new item there's sort of a buffer even if it has zero
1:22
items that might have some space to store that let's say 10.
1:26
I don't know the exact numbers off the top of my head and then you add one and another and it
1:30
knows there's two but there's really memory space for 10 pointers and then once it gets past 10 it's going to
1:35
grow and not just grow by one more byte and one more byte. That would be really tricky to have to copy for every append.
1:41
So it grows maybe by doubling, so it might go from 10 to 20 places and then add the 11th
1:46
and the 12th. It's now surprisingly before it wasn't is now optimized for that case where you're adding an item
1:53
and you have space already because of that doubling of the actual memory that it manages versus the number of pointers
2:01
So most of the time it's just going to be putting pointers into existing memory and resize is required, it's now optimized for that,awesome.