I was wondering how to optimize the performance of async/await codes withTPL(Task Parallel Library).
Therefore, I tested different amount of tasks(10/1000/5000) separately, and each task took 5 seconds.
- The result :
Without TPL | With TPL | |
---|---|---|
100 tasks | 500 sec | 27.91 sec (MaxDegreeOfParallelism = 20) |
1000 tasks | 5000 sec | 135.34 sec (MaxDegreeOfParallelism = 70) |
5000 tasks | 25000 sec | 317.33 sec (MaxDegreeOfParallelism = 130) |
- The testing code :
publicasyncTaskGetTasks(){try{Stopwatchsw=newStopwatch();// startsw.Start();varworkBlock=newActionBlock<int>((a)=>GetTask(a),newExecutionDataflowBlockOptions{//Gets the maximum number of messages that may be processed by the block concurrently(from MSDN)MaxDegreeOfParallelism=5});// 100 tasksforeach(vartaskIndexinEnumerable.Range(1,100)){workBlock.Post(taskIndex);}workBlock.Complete();awaitworkBlock.Completion;// stopsw.Stop();Console.WriteLine($"{sw.ElapsedMilliseconds}ms");}catch{throw;}}publicasyncTaskGetTask(inta){// mock http requestawaitTask.Delay(5000);}
Thank you!
Top comments(1)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse