Python 3.11: A Guided Tour Through Code Transcripts Chapter: Error Handling Improvements and except* Lecture: Performance Demo of Zero Cost Exceptions

0:00 Let's actually test the timing. All of those things were fun. Right? We can run this once here as well.
0:10 That was fun. But let's actually see how long this is going to take. Let's try to run this code and see if it makes any difference whatsoever.
0:19 So what we want to do is we want to call that function troubling method 10 million times and see how
0:25 long it takes. But that number over there will say for. We're gonna go loop over that this many times and I'm gonna say the elapsed time,
0:39 it's gonna be done in some number of milliseconds with two decimal points and digit grouping.
0:45 Let's run and see what happens. We go 922.26 ms I suppose we don't need the decimal points,
0:53 but they don't hurt us there. Do they run it a few more times. There's a decent amount of variation. I gotta tell you,
1:04 screen recording over webcams and the screen puts a bit of a hurt on the computer. But let's give it a shot. Now, you saw it over here in 3.11.
1:14 A few times. Just to get some numbers. Okay, a little faster. Here we go. I'm gonna say eight. Let's pick the lowest number. We got 8 40.
1:25 That was decent. Let's try the exact same code. Just running python 3.10 Still over 1000. What are we getting?
1:39 What's Our best So 1,035, Divided by 840. Well, that's 23% faster or slower for a python, python 3.10 3.11. Not going further back than that.
1:58 Right? Not looking at 3.8 or 3.7, but the very recent most recent version. Right, So this looks like you got about 20% faster.
2:06 Because guess what? We're not paying the price to set up the try block, even when it succeeds. And remember most of the time,
2:13 if you're writing good code and working with somewhat reasonable data,
2:16 it's going to succeed. So why pay the price for the odd exception case when using 3.11? You dont.

Talk Python's Mastodon Michael Kennedy's Mastodon