Why place i/o task before cpu-bound task runs faster than place i/o task after cpu-bound task

2 hours ago 2
ARTICLE AD BOX
using System.Diagnostics; const int TASKS = 100; var mainSw = Stopwatch.StartNew(); var tasks = Enumerable.Range(0, TASKS).Select(i => Task.Run(async () => { await Task.Delay(5000); var sum = 0; for (int j = 1; j <= 100000000; j++) { sum += j; } Console.WriteLine($"done"); return sum; }) ).ToArray(); await Task.WhenAll(tasks); mainSw.Stop(); Console.WriteLine($"\nTotal time: {mainSw.ElapsedMilliseconds}ms");

I run start 100 tasks at the same time. and place Task.Delay(5000) before the for loop. Task.Delay(5000) acts like an i/o task, and the for loop acts like cpu-bound task. When it starts to hits the for loop, it will execute in batch ~ cpu core (8 - 16). so the overal time will be 5s + the longest task of last batch.

if i place Task.Delay(5000) after the for loop, i assume time takes to finish will be roughly the as i place it before the for loop. Because it still starts for loop in batch first, so the overal time taken will be the longest task of last batch + 5s (the delay of last longest task).

But when i test, it seems like if i place Task.Delay(5000) before the for loop, it will run faster than place it after the for loop ~ 200ms.

Can someone helps me to understand deeply how it executes under the hood.

Read Entire Article