延後執行函式
你被指派要加總公司最近 2 週的成本。因為你知道未來會用更多週數重複執行相同的運算,所以你打算用可平行化的方式來撰寫。
最近兩週的品項成本陣列分別為 costs_week_1 和 costs_week_2,而且已經將 numpy 以 np 匯入。
本練習屬於課程
在 Python 中使用 Dask 進行平行程式設計
練習說明
- 匯入
delayed()函式。 - 將
np.sum()延後執行,並用它來建立costs_week_1與costs_week_2的 delayed 加總。 - 將這兩個加總相加,建立另一個代表總和的 delayed 物件。
- 計算並列印這個總和。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the delayed function from Dask
from ____ import ____
# Lazily calculate the sums of costs_week_1 and costs_week_2
sum1 = ____(____)(____)
sum2 = ____
# Add the two delayed sums
total = ____
# Compute and print the final answer
print(____)