SIGN UP MEMBER LOGIN:    
ARTICLE

Parallel.For Loop in .NET 4

Posted by Suresh Paldia Articles | .NET 4.5 November 12, 2010
In this article you will learn how to use Parallel.For Loop in .NET Framework 4.
Reader Level:


Do you have a multiprocessor system? Then, Right click on taskbar and open task manager and click the Performance Tab and notice the performance. You will find something like shown below:

image1.gif

This means there are cores not fully utilized for best performance.

Sequential For loop

for (int i = 0; i < 100; i++)
{
        //Code though independent in each iterations executes sequentially
        a[i] = a[i]*a[i];
}

Multicore-processors machines are now becoming standard. The key to performance improvements is therefore to run a program on multiple processors in parallel.

Introducing TPL

The Task Parallel Library (TPL) is designed to make it much easier for the developer to write code that can automatically use multiple processors. Using the library, you can conveniently express potential parallelism in existing sequential code, where the exposed parallel tasks will be run concurrently on all available processors. Usually this results in significant speedups.

TPL is a major component of the Parallel FX library, the next generation of concurrency support for the Microsoft .NET Framework.

Since the iterations are independent of each other, that is, subsequent iterations do not read state updates made by prior iterations, you can use TPL to run each iteration in parallel on available cores, like this:

Parallel For loop

Parallel.For(0, 100, i =>
                {
                        //This block should contain independent code
                        //It must not update/use value of some variable in
                        //this block from previous iteration
                        //This block should contain a code that takes a huge       
                        //execution time in each iteration
                        //Small complexity code here can result in poor performance
                        a[i] = a[i]*a[i];
                }
            );

image2.gif

Happy Learning...
 

Login to add your contents and source code to this article
share this article :
post comment
 

The namespace to use for using Parallel.For and Parallel.ForEach is : System.Threading.Tasks

A similar Parallel.ForEach loop is:

                     List<int> list = new List<int>();
                     //populate a list
                     Parallel.ForEach(list, item =>
                           { 
                                    DoSomeWork();
                            }
                       );

Posted by Suresh Paldia Nov 12, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor