/// <summary> /// Awaiting each IO bound operation in sequence is wasting time when the resource is under-utilised /// </summary> /// <returns></returns> public Task <TimeSpan> GetMany_AwaitingEach_Bad() { var resourceGetter = new SomeAsyncResource(); return(TimedExecution(async() => { for (int i = 0; i < iterations; i++) { await resourceGetter.GetStringContentAsync().ConfigureAwait(false); } } )); }
/// <summary> /// Fire off multiple requests and await IO completion in parallel. /// However be careful not to overload the remote resource or the local client machine (each tasks uses memory, disk/network etc.) /// Considering batching. /// </summary> /// <returns></returns> public Task <TimeSpan> GetMany_AwaitingAll_IOBound_Good() { var resourceGetter = new SomeAsyncResource(); return(TimedExecution(async() => { var tasks = new List <Task <string> >(); for (int i = 0; i < iterations; i++) { tasks.Add(resourceGetter.GetStringContentAsync()); } await Task.WhenAll(tasks).ConfigureAwait(false); } )); }