Editor

The Best Split for Two Processors

Advanced · Write the code · from Parallel and Distributed Computing: Speedup and Its Limits

From the lesson: What if there are more tasks than processors?

Often there are more tasks than processors. Then each processor works through its share of the tasks one at a time, and the parallel portion lasts as long as the busiest processor. How the tasks are shared out matters. Say a computer with two processors has four independent tasks taking 30, 20, 25 and 15 seconds.

Read this part of the lesson

Write best_two_processor_time(tasks). A computer has two processors and a list of independent task times. Each task runs on exactly one processor, and each processor runs its tasks one after another. Return the least possible time for all the tasks to finish.

best_two_processor_time([20, 30, 40, 50])  ->  70   (20 + 50 and 30 + 40)

There is a neat way to try every split, using binary: with n tasks, each whole number from 0 to 2 ** n - 1, written with n bits, describes one split. A 1 in position i sends task i to processor B, and a 0 sends it to processor A.

def best_two_processor_time(tasks):
# Try every split; keep the smallest finishing time
pass

What Check runs

Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.

RunsExpected output
print(best_two_processor_time([20, 30, 40, 50]))70
print(best_two_processor_time([10, 10, 10]))20
print(best_two_processor_time([3, 3, 4, 5, 5]))10

More Parallel Computing exercises

All Python exercises · 50 practice problems