Loading video player…

Python Timer Functions: An Overview

Python’s time module gives you four different ways to measure time: monotonic(), perf_counter(), process_time(), and time(). And they don’t all agree with each other. One ignores your system clock. One ignores sleep() entirely. One can run backwards.

In six minutes, Mahdi Shadkam-Farrokhi walks through all four with live demos, including winding the system clock back an hour mid-measurement to show which functions notice and which don’t. By the end, you’ll know which function belongs in a timeout, which belongs in a benchmark, which belongs in a profiler, and which belongs in a log line.

Then take it further: the full video course abstracts these functions into a reusable Timer class, extends it to handle multiple timers, turns it into a data class, and finally makes it work as a context manager.

Resources mentioned in this lesson:

Python Timer Functions: Three Ways to Monitor Your Code

Course

Python Timer Functions

Learn how to time your Python code with the time module, then build a reusable Timer class that works as a context manager.

intermediate python stdlib

00:00 Here is a little riddle for you. The older you get, the less you have of it. What is it? The correct answer is money. I mean time. The older you get, the less time you have available.

00:11 So I guess it’s a good idea to be aware of time. And as usual, Python is great at helping us with that. Besides other handy modules and packages, Python’s standard library also contains the time module.

00:24 That means the only line of code that you need to work with time in Python is import time. And once time is present, you can do a bunch of cool stuff with it.

00:34 We have a great reference term article on realpython.com written by Leodanis Pozo Ramos. This reference article shows you the ins and outs of the time module.

00:44 And recently, Mahdi created a video course for Real Python where he specifically shows you how you can leverage the time module to create a timer. The problem is Python’s time module comes with a bunch of timer functions that could help you with that job.

00:59 So which timer function do you use when? Let’s hand it over to Mahdi to give you an overview. The functions we’re looking at today from the built-in time library are monotonic, perf_counter, process_time, and time.

01:12 Let’s get into it. The monotonic function will return a referenceless monotonic clock value. I know that sounds like a lot of words, but basically it just means that the value that you get from this function call doesn’t have a whole lot of meaning on its own.

01:28 The power comes from calling it twice and then getting the difference between those two values, and that will give you the number of seconds that have passed between the two function calls.

01:37 I will note here that the monotonic_ns function works in the exact same way, except it’s at the level of nanoseconds instead of seconds. It is not affected by any kind of system clock updates, and it will include any time that has elapsed during the call to sleep.

01:55 Sleep is a function also in the time module. This function is best used for timeouts, scheduling, and long-term timers. Let’s see how it works. So here I’m calling monotonic once and then storing the value, and then I’m going to go into my system clock and set it back an hour, and this is just to show that if we change the system clock and we call monotonic again, it doesn’t actually affect the end result.

02:18 The difference will show how much time has elapsed, and here we see that calling sleep in between the two function calls will actually result in that time, that waiting time being captured in the difference. perf_counter will also return a referenceless monotonic clock value, except this time it’s at the highest available resolution on the system.

02:41 Everything else works basically the same as monotonic, and that’s why this function is best used for benchmarking and performance profiling. All the same examples I had for monotonic would apply to perf_counter. process_time returns a referenceless monotonic clock, except this time it’s looking at the CPU time, meaning that it’s the amount of time that your CPU is actively executing your code.

03:04 So while it is not affected by system clock updates, process_time will ignore any time that has elapsed during the call to sleep, as well as any IO or input output where the system is waiting on input.

03:17 This particular function is good at evaluating computational complexity or code efficiency. Let’s see how it works. So here I have a call to sleep in the middle of the two function calls, and we see here that the difference is basically zero seconds because sleep doesn’t really do much.

03:34 The CPU isn’t really processing anything. Similarly, if we call input and the system will hang and wait for user input, you could be there for an hour. It doesn’t really matter because the CPU really isn’t taking much time to process that, and so calling time again will result in not really much difference.

03:52 There really isn’t much CPU processing going on. Lastly, we have the time function. This one is the one that’s quite a bit different. It returns the wall clock time.

04:03 This is the number of seconds from this arbitrary zero point of standard time, also known as epoch, and this is something that actually is affected by the system clock because it actually represents something with more meaning than the others.

04:19 Where there was this referenceless monotonic clock before, this one actually returns a value that has meaning, the number of seconds since that zero point in time.

04:29 So naturally, this function is affected by system clock updates. This is useful because you can actually use the value to generate human-readable timestamps, which makes it really good for logging.

04:41 Let’s see how this works. So here I’m calling time and then sleep and then time again. Naturally, this is going to capture the amount of time during the sleep, and what’s interesting is the literal value that you get from calling the function can be formatted to be human-readable.

04:58 And then if we call this time function again and then go in our system and change the clock back an hour and then call it again, we get this unusual behavior where it looks like we went back in time.

05:10 Okay, so that’s it for all the timer functions. Let’s check out using this in a timer class. In this course, we’ll go over various timer functions from the time module.

05:19 We’ll be abstracting those functions into a timer class, and then we’ll be looking at what it would look like to have the timer class as a context manager.

05:28 We’ll go over the timer functions first. That’s what Mahdi was already showing you here on YouTube. We’ll look at an initial timer class, then we’ll look at what multiple timers would look like.

05:39 We’ll then be enhancing our timer class to work as a data class. We’ll explore context managers as well as looking at how our timer class could be made into a context manager and then seeing it in action.

05:50 So if you value time as much as I do, check out the video course by Mahdi. I’ve linked it here somewhere and, of course, in the description below.

Become a Member to join the conversation.