I am running Parallel.For loop in 2 different ways:
Not writing any async/await but just calling
Task ReadSensorAsync(int):var watch = System.Diagnostics.Stopwatch.StartNew(); Parallel.For(3, 38, index => { ReadSensorsAsync(index); }); watch.Stop(); Console.WriteLine("Parallel finished in {0} ms", watch.ElapsedMilliseconds);The timing shows
1msRunning with
async/awaitvar watch = System.Diagnostics.Stopwatch.StartNew(); Parallel.For(3, 38, async index => { await ReadSensorsAsync(index); }); watch.Stop(); Console.WriteLine("Parallel finished in {0} ms", watch.ElapsedMilliseconds);This shows
2-5ms.
Question is why there is such difference and which way is proper to use.