Python 3.11: A Guided Tour Through Code Transcripts
Chapter: Python 3.11 Performance
Lecture: Timing the More Specialized Code
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Notice we have this function called round trip that does basically the same thing here without the print statements and the
0:07
asserts, if we time how that function runs, we should be able to see if there's any sort of improvement. Right? Let's write one more function here.
0:17
We'll put it maybe by the test conversions, we'll say a def test speed.
0:23
And let's go in here and say we want to run this a million times but we've got all these test values, We want to use eight or nine or something,
0:30
so we're going to divide that back so that when we loop over it and then loop over those values,
0:35
the ultimate result is around a million operations. Okay, So what we're gonna do is instead of doing this other one down here,
0:45
we're gonna say test speed. Now I need to put this into both because we want to see it before
0:49
and after. Right. All right, now let's go ahead and run the this one 149 milliseconds.
1:11
We'll run it a few more times. Go, it's around 100 and 140 145 150. Let's run this one. This is the one we've helped the specializer do more.
1:24
113 115 1 18. Oh yeah, 107. awesome. So we went from around 145. 150 to 108. What is that? 35%,
1:41
improvement roughly awesome. That is really cool. And now remember what we're doing is not running this under an
1:49
old version of python and then the newest version of python and saying, look, the new version is faster. Both of these were running under Python 3.11.
1:57
It's just a matter of did we use the information that we got to write our code slightly differently so that
2:06
the specialized in adaptive interpreter can run with it or do we just leave it the most naive way that we
2:13
might have written this code and then hope that it ran fast.
2:18
Now. This is not something I would go crazy all over my program and just make sure I'm doing this
2:23
everywhere. I wouldn't worry about that. But if you're profiling your code and then you find a spot like
2:30
this part is doing a little bit of math and it's run a lot of times.
2:36
So it really matters that this little bit, this little portion of the math in my program matters.
2:42
You know, maybe a couple of parentheses conversion, pre conversion to a floating point number,
2:47
something like that would help you a ton Now. The folks over on the faster C python team Brandt Bucher
2:54
and folks said look, the intent of this tool is not really to help you rewrite your code.
3:00
So it's faster it's to help them understand the adaptive interpreter on different code so they can continue to make it
3:08
faster and that's great. That'll just make existing code go faster and faster But you know,
3:13
this is pretty compelling 35% faster by putting a couple of parentheses in floating point dot 0.0 on there.
3:22
That's a really easy way to make your code go faster. And if it really does, I don't see why you wouldn't want to do this again in a limited few
3:30
places where there's really hot loops, really important calculations that run over and over.
3:37
So that's the specializing adaptive interpreter in Python 3.11.
3:42
Mostly it's automatic, but you can see that you can write your code be more in line with what it
3:48
can help you with or kind of out of line where it's decides it can help you.
3:52
So go play with your code again only where it matters. Dont over complicate it.