Detecting embarrassingly parallel workloads
Applying a simple function over a loop is a good example of a so-called embarrassingly parallel workload, which means that an easy optimization that can result in improved performance is to use multithreading or multiprocessing techniques to speed up the calculations. More broadly, the basic criterion you should check before trying to implement a parallelization optimization is that the sub-units of work making up your task are completely independent. In the previous example, the distance calculation for cities A and B is completely independent of the B to C case. You can find a simple example of an embarrassingly parallel workload in the repository file Chapter 2/embarrassingly.py, the code being a simple math operation:
import time
import multiprocessing
def square_number(n):
time.sleep(0.001)
return n * n
def serial_square_list(numbers):
return [square_number(n) for n in numbers]
def parallel_square_list(numbers, num_processes...