Introducing Python performance benchmarking
Every software solution has associated costs. Solutions consume computational resources and also have an associated level of complexity that has implications for runtime performance. The parameters relevant to measuring Python code execution include time taken, CPU cycles used, memory allocation, network bandwidth, and persistent storage usage. We are going to focus on time and memory. Time can be measured in many ways; usually you can obtain measures of CPU time and wall time. CPU time measures the duration of actual computation, excluding the overheads of waiting and task switching (which, as we have already shown, has important consequences). Wall time, meanwhile, includes all the subtasks required to execute a piece of code.
The following code uses the perf_counter() function from the time module to compare two implementations of an approximation function for calculating the value of pi (3.14159265...):
import time
def pi_approximation...