Python 3.11: A Guided Tour Through Code Transcripts
Chapter: Python 3.11 Performance
Lecture: Python's Specializing Interpreter in Action

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's start with the code that is really similar to what you would find on the Git hub specialist page as
0:06 the example. So real simple, what we have is a function that takes Fahrenheit degrees and converts them to
0:12 Celsius. Of course that's subtract the 32 to get it to the zero point and then multiply by 5/9 to
0:20 convert from the step size of Fahrenheit to Celsius. And of course you can do that in reverse the other
0:26 way. There's a bunch of test values that you might want to test and see how they get converted here
0:33 We have this function called test conversions and what it does is it loops over and it just checks to
0:39 make sure that if we go from given a number a degree from Fahrenheit to Celsius and then Celsius back to
0:47 Fahrenheit that those are the same as well as reverse Celsius.
0:51 Fahrenheit then Fahrenheit, Celsius is the same and just prints out some simple things there.
0:58 Alright let's go ahead and run this I suppose. See what we get here.
1:02 You can see looks like it's working Zero Fahrenheit is negative 18 ish Celsius and 32 is of course zero
1:11 Celsius. There's some other ones as well but you know you can see these test values putting out reasonable numbers
1:18 it looks like the code works. This is not about making the code work,
1:23 it's about understanding what the specializing adaptive interpreter can do with it.
1:28 So for example, up here notice we have a floating point being with an integer subtracted from it.
1:36 It turns out the way the specializing interpreter works, how it adapts for the moment is it looks for the same type. So do I have a float in a float?
1:46 Well in that case we can speed those types of operations up.
1:50 It'll help us look through this code and see if there's a way we can give a little nudge to the
1:55 specializing adaptive interpreter to make it work better. So let's go over here to the terminal and here we are
2:05 in this specialist start. I have a start and I'm gonna have a final because we're going to change this
2:10 in a moment. In fact, let's go ahead and give us the final because we're going to make changes to it right now. It's the same. Right?
2:18 So look at this, let's run special list in the final like that. What it's going to do is it's gonna parse the file,
2:29 understand what's happening and then it will generate an html report for us.
2:36 So here we have that report for our code. This stuff down here just ignored. It doesn't matter. This is not something we can optimize.
2:44 Like we don't care about assert or strings. Similarly for looping over our test values,
2:48 it's just those two functions that we might consider performance critical, doing our Math and we want to pay attention to them.
2:56 Notice two things here, there's green. Green is optimized, we can do all sorts of cool stuff with that,
3:02 although not much yet. Yellow is a little bit of an optimization but not massive. And then pink is we couldn't do anything with that.
3:11 So Pink is not good. Yellow, not great. Alright, let's look at this. Why is this one not optimized again,
3:22 a float and an end. It seems like well, maybe the system could do that for whatever reason it doesn't at the moment. So if I just put a 32.0,
3:32 well all of a sudden that's a float on float operation and it makes no change right. The result of this is going to be a floating point number anyway.
3:40 So if I go ahead and get ahead of the game and make that a float, it's not going to hurt it. So let's go and run that specialist again.
3:51 Move this over so it doesn't get hidden by the presentation assistant.
3:56 Look at that. This line is now going to be fully adapted to work in a higher performance.
4:02 More specific version of python byte code that knows about floating point numbers,
4:08 not arbitrary edition because if you add strings, you get something different. If you add lists, you get something different than if you add floats.
4:16 So it doesn't have to be general like that. It's like, nope, these are numbers, let's do that similarly.
4:22 Let's look at this one here, we have a float times an end.
4:27 This is the order of operations. And then divided by an int we can make a super minor change here
4:36 like this. We can put parentheses here and just basically change the order.
4:41 This is going to become a constant that gets evaluated at parse time by python into a float. What do we get if we run this now? Check it out,
4:54 cycling back and forth. You can see now that float to Celsius thing is fully optimized. And let's go ahead and apply that for the Celsius 1 as well.
5:05 We can force that constant to be computed before and we can go ahead and turn that into the float that
5:11 it's going to be anyway. There it is. And forget about the stuff below these are the two functions
5:18 we care about. Let's see that they still run and give the same numbers, you know? Sure enough. Here's the boiling point tests.
5:28 Here's the zero point tests. Different directions, same still works the same obviously.
5:37 But we've given it a little bit of help and unified the types involved here and that's going to make the specialize adaptive interpreter work a little bit better.


Talk Python's Mastodon Michael Kennedy's Mastodon