none
Difference between For & Parallel.For in c#

    Question

  • 1) what is Parallel.For loop ?

    2) what is the usage of Parallel.For loop ?

    3) when People use Parallel.For instead of normal for loop?

    4) how Parallel.For loop is different from normal for loop?

    suppose i am read a data table value in for loop like

    for(int x=0; x<=mydatatable.rows.count-1;x++)

    {

    }

    in what kind of situation one should use Parallel.For loop to read data from datatable ?

    looking for good discussion the difference between for & Parallel.For loop ? and their usage difference. please explain all my points with sample code & example . thanks

    Sunday, May 19, 2013 6:17 PM

Answers

  • 1) What is Parallel.For loop?

    Parallel.For loop is used for Parallelism, which is taking a certain task and dividing it into a set of related tasks to be executed concurrently.

    The Parallel Programming model of .NET 4.0 is composed of Task Parallel Library (TPL). Parallel.For is one of the methods exposed by the Parallel class of TPL.

    2) What is the usage of Parallel.For loop?

    Use the Parallel Loop pattern when you need to perform the same independent operation for each element of a collection or for a fixed number of iterations.

    Parallel.For loop, when executed over multiple cores, the body of the loop gets executed in parallel over multiple threads.

    3) When People use Parallel.For instead of normal for loop?

    You should only use parallel when you have a long, intensive process. If you use it for small, easy loops & tasks you could actually slow down the overall process. Use the Parallel.For method to iterate over a range of integer indices.

    4) How Parallel.For loop is different from normal for loop?

    Parallel.For and normal For loop both share the similar syntax, but the parallel loop runs faster on a computer that has available cores.

    Another difference is that, unlike a sequential loop, the order of execution isn't defined for a parallel loop. Steps often take place at the same time, in parallel.

    • You can't mutate a DataTable from 2 different threads; it will throw error. DataTable makes no attempt to be thread-safe. So: don't do that. Just do this from one thread.
    • Also, DataTable.Rows returns a DataRowCollection which only implements IEnumerable, not IEnumerable<DataRow>. Use the AsEnumerable() extension method on DataTable (from DataTableExtensions) instead:

    Parallel.ForEach(dt.AsEnumerable(), drow =>

    {

    ...

    });


    Thanks, AT

    • Marked as answer by Mou_kolkata Tuesday, May 21, 2013 1:37 PM
    Monday, May 20, 2013 9:10 AM

All replies

  • 1) What is Parallel.For loop?

    Parallel.For loop is used for Parallelism, which is taking a certain task and dividing it into a set of related tasks to be executed concurrently.

    The Parallel Programming model of .NET 4.0 is composed of Task Parallel Library (TPL). Parallel.For is one of the methods exposed by the Parallel class of TPL.

    2) What is the usage of Parallel.For loop?

    Use the Parallel Loop pattern when you need to perform the same independent operation for each element of a collection or for a fixed number of iterations.

    Parallel.For loop, when executed over multiple cores, the body of the loop gets executed in parallel over multiple threads.

    3) When People use Parallel.For instead of normal for loop?

    You should only use parallel when you have a long, intensive process. If you use it for small, easy loops & tasks you could actually slow down the overall process. Use the Parallel.For method to iterate over a range of integer indices.

    4) How Parallel.For loop is different from normal for loop?

    Parallel.For and normal For loop both share the similar syntax, but the parallel loop runs faster on a computer that has available cores.

    Another difference is that, unlike a sequential loop, the order of execution isn't defined for a parallel loop. Steps often take place at the same time, in parallel.

    • You can't mutate a DataTable from 2 different threads; it will throw error. DataTable makes no attempt to be thread-safe. So: don't do that. Just do this from one thread.
    • Also, DataTable.Rows returns a DataRowCollection which only implements IEnumerable, not IEnumerable<DataRow>. Use the AsEnumerable() extension method on DataTable (from DataTableExtensions) instead:

    Parallel.ForEach(dt.AsEnumerable(), drow =>

    {

    ...

    });


    Thanks, AT

    • Marked as answer by Mou_kolkata Tuesday, May 21, 2013 1:37 PM
    Monday, May 20, 2013 9:10 AM
  • you said You should only use parallel when you have a long, intensive process. If you use it for small, easy loops & tasks you could actually slow down the overall process. Use the Parallel.For method to iterate over a range of integer indices.

    can u please show me when to use Parallel.ForEach or Parallel.For with sample code and sample situation when normal for loop will not be appropriate. thanks

    Tuesday, May 21, 2013 1:38 PM
  • The statemeans means that Parallel loops utilize multiple system core and threads so its advisable to use them when there are intensive and time consuming processes otherwise, due to their nature, Parallel processes may slow down the application. Combination of a small amount of work and undesirable cache effects can result in performance degradation in parallel loops. Don't think there is anything appropriate or not appropriate we only have to decide when to use it or not.

    It is a common and good practice to do some sort of profiling and performance test before you start optimizing some code and right after. Performance tests will show how much you got from optimization, and if you got anything at all. In many cases, you may believe that you are optimizing your code, making it run faster, but in actuality, you may get a performance decrease. In some cases, your paralleled code may perform slower. This may happen due to the fact that the amount of paralleled work is very small and the amount of time spent on threads synchronization is much greater.

    Having said that, there are a few more points which you should consider before opting Parallel.For.

    • The parallel computations extension is targeted for .NET framework 3.5. The 3.5 version is really nice, and provides a lot, but some applications may still want to support earlier .NET framework versions, like 2.0, for example, which makes it impossible for them to use this extension.
    • The parallel computations extension is not yet included into the standard .NET framework installation, but is provided as a separate component, which should be installed on a target system as well. This may complicate the distribution of your product a bit, requiring you to provide special instructions to users specifying all the dependencies, or to incorporate the installation of the extension into your product’s installation.
    • The parallel computations extension provided by Microsoft is aimed to run on Windows systems. However, if you are developing cross platform applications, which should also run on Linux, for example, under the Mono environment, then you will be left without the nice paralleling features. Although parallel extensions are going to appear in Mono soon, they are not yet available in the current version.

    Thanks, AT

    Wednesday, May 22, 2013 9:45 AM