Python time for Performance Measurement
In this last lesson, I’m going to show you a couple of useful functions that the Python
time module provides you for working with the performance of your code, measuring it and modifying it as necessary.
The two functions that I’ll be showing you are as follows.
time.perf_counter(), which is essentially a super precise little stopwatch, and just like a stopwatch, the
perf_counter doesn’t actually give you a real time, per se.
You don’t know whether those 10 seconds were from 3:00 PM to 3:00 PM and 10 seconds or midnight to midnight and 10 seconds. So that’s what
perf_counter() does, and then
sleep() allows you to pause the execution of a thread of a Python program for the specified number of seconds.
This time output is obviously not the same as
time.time(), so it doesn’t represent the time since the epoch. What it represents instead is the very precise time since a recent fixed point, which means that it’s useless to determine the date, but what it’s really useful for is to say something like
a = time.perf_counter(), and then shortly afterward, you could say
b = time.perf_counter(), and then you can say
b - a and get a very precise account of the time delta
And then what you can do there is you can start at the beginning of the function, you can start a
perf_counter, then you can do some kind of unexciting work or very exciting work, depending on what you’re doing.
02:48 Then when you call it, it’ll hang for a little bit and then it’ll tell you that this took exactly, or not perfectly exactly, but very, very close to precisely, this amount of time: 0.73995 seconds.
03:01 So that’s really useful because there are a lot of applications where you need, really, precision timing. You know, science, engineering, all those sorts of things you require the time that is this precise.
So that’s really useful. Another function that’s useful is
time.sleep(). And it does exactly what you would think it does: it makes the program essentially go to sleep for however many seconds you pass in, and that just means it’s not doing anything for this time.
03:25 So if I pass it in 5, you can count down from 5 and it’ll just essentially do nothing for that time. Why is this useful? Well, there are a lot of occasions where it’s useful to do nothing for specified times, right? So think about rate limiting, for example.
03:41 If you’re querying some kind of server, you probably don’t want to always be querying that server. You want to give some gaps in there to allow your program to do other things or simply to allow the server to be able to give you that information and not have its resources constantly tied up by giving you back the information you’re requesting. So that’s one example, but there are many, many others where sleeping is actually something quite useful.
And then I’m actually gonna use
perf_counter() again, just to make sure that this thing is sleeping for the right amount of time. So you can say
time.sleep(x) and then just return, again, the
perf_counter minus the start.
And so you can run that and let’s give it, maybe, 5 seconds and as you can see, it will sleep for that amount of time and then it will return the
perf_counter, which, as you can see, is pretty much infinitesimally close to 5 seconds.
So some of that variation might just be the time it takes to call
perf_counter or other system factors that make this run a little bit longer than exactly 5 seconds. But as you can see, it’s vanishingly small, the actual difference.
So that’s how
time.sleep() works. It’s really simple, but it can be useful in a lot of surprising ways. I can’t tell you the number of times where I’ve had an application where I’ve said, “Oh, why is this performing so badly?” Well, it’s because it’s querying some server and I’m not giving it enough time to, kind of, react. Sleep can be very helpful in unexpected ways, so it’s good to know.
Become a Member to join the conversation.