Learning Python with Advent of Code Walkthroughs

Dazbo's Advent of Code solutions, written in Python

The Python Journey - Timing and Progress

Argand Plot

Useful Links

TimeString format()f-stringstqdm (progress bar)

Page Contents

Time Module

The time module is a convenient and easy way to measure how long something takes to run.

Simply call time.perf_counter() before and after executing something. The difference between the two responses is the number of seconds taken.

In the example below, I’m using the sleep() method to simulate a long running process. Here, I’m telling Python to sleep for 2 seconds.

import time

t1 = time.perf_counter()
time.sleep(2) # sleep for 2 seconds
t2 = time.perf_counter()
print(f"Execution time: {t2 - t1:0.4f} seconds")

Output:

Execution time: 2.0037 seconds

Note how I’m displaying the elapsed time with 4 decimal places of precision, using the :0.4f format within an f-string.

Here I’ll change the program to simulate waiting for 10 milliseconds:

import time

t1 = time.perf_counter()
time.sleep(0.01) # sleep for 0.01s (i.e. 10ms)
t2 = time.perf_counter()
print(f"Execution time: {t2 - t1:0.4f} seconds")

The output:

Execution time: 0.0109 seconds

Progress Bar with tqdm

The tqdm class is an awesome tool for creating a dynamic progress bar. This is really useful if you have a long-running process, and you want to see a real-time indicator on how much of the process is completed, and an estimate of how long is left.

It’s incredibly easy to use. In its simplest form, you simply wrap tqdm around any iterable in a loop.

First, if you haven’t yet installed tqdm into your environment, you’ll need to install it with pip.

py -m pip install tqdm

First, let’s simulate a long running process:

from time import sleep

steps = 10

for step in range(steps):
    sleep(0.5)

This program iterates through 10 steps, and pauses for 0.5s on each step. So obviously, it will take about 5 seconds to run. During that time, there is no visual indication of progress.

To use tqdm, all you have to do is wrap tqdm around the iterable, like this:

from time import sleep
from tqdm import tqdm

steps = 10

for step in tqdm(range(steps)):
    sleep(0.5)

Progress Bar

Examples

See: