Python Performance
Python Speed Performance
Measuring code performance is vital, especially in the advancement of larger and more complex programs. In Python, there are various ways of measuring performance metrics.
This page looks specifically at speed, and it covers four different ways of doing so.
Note that there is a difference between measuring execution of code and processing time. When testing the time for code execution, there are factors involved that may impact the performance, such as background processes (e.g. operating system, other software running, etc.). In contrast, processing time is the time it takes the CPU to process the code execution.
All four measuring techniques involve importing Python built-in packages (time, datetime, timeit, and cProfile).
Time
The package “time” can measure basic time difference between the start of the code execution and the end. Note that this will measure the code execution time, including all other factors, and not the processing time of the CPU.
import time # record start time start = time.time() # multiple "list" and save the result in "sum" list = [1, 2, 3, 4, 5, 6, 7] sum = 0 for i in list: sum = 10000 * list # record end time end = time.time() # difference between start and end time difference = end - start print(difference, 'seconds') # Output 0.0009930133819580078 seconds
The result in the above example is less than a second, as it processes a very basic function. In case of even simpler process, for instance only printing a number, the outcome will show as 0:0 seconds.
The code imports “time”, then records the start time in a variable. Furthermore, it performs a simple for loop, records the end time in a variable, and calculates the difference between the start and end timing. And finally, it prints the result in seconds.
This package can also measure CPU processing time. It works the same way as the above example.
Note: increase multiplication from 10,000 to 10,000,000.
import time # record start time start = time.process_time() # multiple "list" and save the result in "sum" list = [1, 2, 3, 4, 5, 6, 7] sum = 0 for i in list: sum = 10000000 * list # record end time end = time.process_time() # difference between start and end time difference = end - start print(difference, 'seconds') # Output 1.359375 seconds
If the processing performance applies to the first multiplication of only 10,000, the outcome will show as 0.0 seconds. This is due to the measurement of only CPU processing (which does not include any other factors).
Datetime
“Datetime” works very similarly to the “time” package.
Below is an example of “datetime”.
import datetime # record start time start = datetime.datetime.now() # multiple "list" and save the result in "sum" list = [1, 2, 3, 4, 5, 6, 7] sum = 0 for i in list: sum = 10000000 * list # record end time end = datetime.datetime.now() # difference between start and end time difference = end - start print(difference, 'seconds') # Output 0:00:01.455151 seconds
The difference is in the package name, time declaration, and outcome structure. Note that this scenario measures the 10m multiplication.
Timeit
The package “timeit” is another technique of measuring speed performance. The following is an example of its usage.
import timeit list = [1, 2, 3, 4, 5, 6, 7] sum = 0 def multiplication(): for i in list: sum = 10000000 * list # function of timeit outcome = timeit.timeit(stmt='multiplication()', globals=globals(), number=5) print(outcome, "seconds") # Output 7.28339309990406 seconds
The “globals” argument provides access of names outside of the “timeit” function. The argument “number” refers to the number of iterations the function does. If 1, the function performs the speed test once. In the above instance, the performance test repeats (iterates) 5 times, hence the longer waiting time.
cProfile
The “cProfile” package offers a summary of more sophisticated statistics of the speed performance of the code.
Below is an example of the usage of cProfile.
import cProfile list = [1, 2, 3, 4, 5, 6, 7] sum = 0 def multiplication(): for i in list: sum = 10000000 * list # function of cProfile c = cProfile.run("multiplication()")
4 function calls in 1.458 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 1.390 1.390 1.390 1.390 1908901341.py:6(multiplication) 1 0.068 0.068 1.458 1.458 <string>:1(<module>) 1 0.000 0.000 1.458 1.458 {built-in method builtins.exec} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}