Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Tingwei
Tingwei

Posted on

     

Optimizing async/await codes with TPL

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 TPLWith TPL
100 tasks500 sec27.91 sec (MaxDegreeOfParallelism = 20)
1000 tasks5000 sec135.34 sec (MaxDegreeOfParallelism = 70)
5000 tasks25000 sec317.33 sec (MaxDegreeOfParallelism = 130)
  • The testing code :

ActionBlock

MaxDegreeOfParallelism

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);}
Enter fullscreen modeExit fullscreen mode

Thank you!

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
tingwei628 profile image
Tingwei
A journey of self-discovery
  • Joined

Here's another greatpost by@cgillum to discuss about the similar topic.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A journey of self-discovery
  • Joined

More fromTingwei

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp