Fragrance
|
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.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
|
Categories
All
Archives
May 2020
|