Saturday, July 18, 2009

Sequential vs Parallel For loop in C# and .NET 4

Sequential vs Parallel For loop in C# and .NET 4

The Task Parallel Library (TPL) in VS2010 & .NET Framework 4 Beta1 is cool! This helps improve the productivity by little change to the code.

Results from printing the Prime numbers for first 1000, 5000, 10k, 50k, 100k, 500k, 1mil..numbers etc using the regular (sequential) and Parallel For Loop
Results may vary when tried on a different CPU.
Mine is an Intel Core2 Duo CPU T9300 2.50 GHz and 4.00 GB RAM

Build a console app in VS2010 and .NET 4. Add the namespaces System.Threading and System.Threading.Tasks
Try the program using each of these for loops and check the performance.

Sequential (traditional) For:

int iRange = 1000000;
for (int i = 0; i < iRange; i++)

Parallel For:

int iRange = 1000000;
Parallel.For(0, iRange, (i) => CheckForPrime(i));

More Info on TPL :

No comments: