Code Mahek
  • Blog
  • About
  • Contact
  • Random Clicks
  • Resume
  • Ava Chat Bot

Fragrance
​of Programming

Parallel.ForEach vs Task.Factory.StartNew

5/11/2017

Comments

 
Recently in my project we had a performance issue, so our teams were on our toe to decrease application start-up. While analysing the code I came across a “for loop” which was creating the problem. Collection was very big and secondly it was performing some long running task. So I had two option two increase the performance:
  • Using Task and
  • Using Parallel.ForEach
Developer might be thinking both might be performing exactly the same and both might be using the ThreadPool. But in reality it works very differently though results will be exactly the same.
Using Task.Factory.StartNew

    
So if you see the above code, I am looping the collection and scheduling a single Task per item in the collection. This particular code will introduce far more overhead especially if the collection is very large and in that case overall runtimes is very slower. Also above code will run in asynchronous manner
Using Parallel.ForEach

    
Now if you see second block of code, it’s a simple Parallel.ForEach call. Internally it uses a Partitioner<T> to distribute your collection into work items. But it won’t create one task per item rather it will create a batch which will lower the overhead. As whole block Parallel.ForEach runs in synchronous manner i.e. block of code after Parallel.ForEach will run only after it's completed.

And in case if you want to run the Parallel.ForEach in asynchronous manner then we can modify the code as shown below. By implementing as shown below you are collaborating the power of Async plus the power of Parallel.ForEach.

Including both Parallel.ForEach and Task.Factory.StartNew

    
Comments
comments powered by Disqus

    RSS Feed

    Categories

    All
    Azure
    C#
    Cloud
    CodeProject
    CSharp
    DotNet Core
    Jetbrains
    .NET
    Rider
    Source Generators
    SQL
    Tech News
    Ubuntu
    Visual Studio 2015
    Visual Studio 2019
    Windows
    Windows 10

    Archives

    May 2020
    February 2019
    January 2019
    December 2018
    May 2018
    December 2017
    November 2017
    October 2017
    September 2017
    July 2017
    May 2017
    April 2017
    May 2015
    April 2015
    March 2015

    View my profile on LinkedIn

Location

Contact Us

    Subscribe Today!

Submit
  • Blog
  • About
  • Contact
  • Random Clicks
  • Resume
  • Ava Chat Bot