Parallel Programming in .net 4.0

10. September 2009

One of the new features added to the .net 4 framework is support for parallel computing. Per MSDN:

The manycore shift presents an unprecedented business opportunity for developers to design new software experiences that take advantage of the performance power of manycore architectures. At the same time, parallel programming is complex, difficult and labor-intensive, for even the most skilled developers….

Microsoft’s goal is to increase productivity by encapsulating complexity, so developers can focus on solving business problems.

I’m not an expert in this subject (and I don’t pretend to be!), but I had some spare time the other day and decided to play around a bit.

The test was simple - create a new GUID, and hash it using the MD5 algorithm. Repeat this 30,000,000 times. First up was a standard for loop.

for (int i = 0; i < 30000000; i++) 
{ 
    using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) 
    {  
        string value = Guid.NewGuid().ToString(); 
        md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(value)); 
    } 
} 

It took 4 minutes, 58 seconds and if you look at the CPU usage, you can see only 1 core was used. Next up was the parallel  for loop:

Parallel.For(0, 30000000, delegate(int i) 
{ 
    using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) 
    { 
        string value = Guid.NewGuid().ToString(); 
        md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(value)); 
    } 
}); 

This time around 30 million GUIDs were created and hashed in 1 minute, 17 seconds. Taking a look at the CPU usage, we can see that the framework automatically farmed the work out across every core.

I haven’t looked into how well the Parallel FX handles thread safety, or how well it performs under more realistic scenarios; but so far I am impressed with what I see. Yet another great move coming out of the .net team!

Resources:
Parallel Computing Developer Center
Parallel Programming in the .NET Framework
Optimize Managed Code For Multi-Core Machines
Running Queries On Multi-Core Processors
PFX Team Blog


short link to this post:

.net ,

Comments

waggi
waggi
12/1/2009 1:18:17 PM #
Great post.

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview
Loading