Sep
10
2009

Parallel Programming in .net 4.0

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

Comments (1) -

waggi

Great post.

Add comment




biuquote
Loading


AdSense

Software engineer by hobby and trade. When I am not sitting in front of a computer, you can find me playing with my kids. I am lucky enough to be married to my best friend and high school sweetheart. Life couldn't be better!

 

All content is mine, not my employers

Chronology

Tweets

stumped over a design in my app...:(
1 day ago via Silver Bird
RT @ramseyshow: Blake: US adds 243,000 jobs in January and unemployment rate drops to 8.3%, lowest in three years. (AP)
3 days ago via rowi
RT @jnonis: Amen! Walking with ppl does! RT @stephenNcollins I dare say that boycotting a business in the name of Jesus has never won an ...
3 days ago via rowi
@danctheduck wife and I are big tea drinkers though. That or water is about all we have
3 days ago via WindowsLive
@danctheduck lol. We go through 3 pots/day (pot is 30oz) brew at least 3 times or else It gets pricy :) breville tea maker ftw
3 days ago via WindowsLive
@danctheduck how do u brew your TKY? Usually do mine at 195 for 3:30
3 days ago via rowi
@danctheduck wait...now I see it. Stopping by next time I'm in seattle
3 days ago via rowi
Follow me on Twitter