DEV Community

Discussion on: A Simple Trick to Boost Performance of Async Code in C#

Collapse
 
shayanfiroozi profile image
Shayan Firoozi • Edited
  • Parallel class is a real parallelism which does your job(s) with some smart threads ! ( specially in .NET 5)
    when a thread finished its job , it will help the other thread(s) !

  • Task.WhenAll is more concurrent not parallel.
    in some cases WhenAll run faster , but in real-time apps with high CPU bound operations we should use Parallel class.
    Also WhenAll creates thousands of tasks which make an high overload on CPU.

  • Here is a Becnhmark :

dev-to-uploads.s3.amazonaws.com/up...

"CreateWords_Parallel/WhenAll" method creates 100,000 random encrypted string.

Please note that how many Tasks been created in "Completed Work Item" column.
28 vs 100,000 !!

Parallel.For done its job with just 28 threads , but Task.WhenAll done it by 100_000 tasks ;)

That's the real difference between them.