nus-cs2030 / 2021-s1

28 stars 48 forks source link

Efficiency of Asynchronous Programming #509

Open sushmit98 opened 3 years ago

sushmit98 commented 3 years ago

Hi everoyone, I wanted to ask, for the following question, is C a more efficient way to do it or D? What is the difference between returning f1.join() + f2.compute() vs f2.compute() + f1.join()? Why should the order of addition matter, since both of them will have to wait for each other either ways? Would be glad if someone could shed some light on this for me. Thank you!

Topic:

Parallel Programming, Asynchronous Programming

image

gabrielloye commented 3 years ago

D is more efficient. For C, since f1.join() comes first in the addition, we'll have to wait for f1 to complete (join()) before we run f2.compute(), making it sequential f1 -> f2. Whereas for D, while f1 is running asynchronously on another thread, we run f2.compute() since it comes first in the addition and it does not need to wait for f1 to complete. In this case, f2 and f1 can run in parallel and we combine both when both are complete.

sushmit98 commented 3 years ago

Alright I understand! Thanks Gabriel!