Loading video player…

Understanding Python Timers

00:00 The functions we’re looking at today from the built-in time library are monotonic(), perf_counter(), process_time(), and time(). Let’s get into it.

00:09 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.

00:23 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.

00:33 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().

00:51 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.

01:01 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.

01:14 The difference will show how much time has elapsed.

01:19 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.

01:30 perf_counter() will also return a referenceless monotonic clock value, except this time it’s at the highest available resolution on the system.

01:39 Everything else works basically the same as monotonic(), and that’s why this function is best used for benchmarking and performance profiling.

01:46 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.

02:03 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 I/O or input-output where the system is waiting on input.

02:16 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, the CPU isn’t really processing anything.

02:37 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 process_time() again will result in not really much difference, there really isn’t much CPU processing going on.

02:56 Lastly, we have the time() function. This one is the one that’s quite a bit different. It returns the wall-clock time. 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.

03:18 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. 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.

03:40 Let’s see how this works.

03:42 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.

03:58 And then if we call the 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.

04:11 Okay, so that’s it for all the timer functions. Let’s check out using this in a Timer class.

Become a Member to join the conversation.