Well maybe not 1 line of code but the new Parallel library in .NET 4.0 makes it dead simple to write parallel processes in our code. Seriously, this new library is really cool and offers tons of new possibilities. The main thing that I like about the library is it makes it so easy to take advantage mulitple processors. Most servers these days, and for that matter personal PCs, have multiple processors. How often are we leveraging those processors in the programs that we write? I bet less often then we should be. Well, Microsoft has give us the capability to do it with ease. No excuses now I guess.
Lets take a look at a really “pre-canned” simple example. Lets say we have a directory filled with a bunch of files that we need to process. The process for each file may take a long period of time. Let’s compare how we might process the files using the good old for loop with how we might do it using the Parallel class.
List<File> fileList;
fileList = this.loadFilelList( someDirectoryName );
foreach (File file in FileNamesList)
{
FileProcessor.ProcessFile(file);
}
So, if the ProcessFile method take 10 seconds and we have 100 files well that’s a long time ( 1000 seconds for all math wizards ). I am sure there is Big O notation to represent this performance. I think of it as “damn it take long” and the more files I have the longer it takes.
Now what if we could distribute that processing across multiple processors simultaneously. Done and done.
Parallel.ForEach(fileNameList, (File file) => {
FileProcessor.ProcessFile(file);
});
What this is doing is that each ProcessFile method is “farmed” off to whatever core processor is available. If we have 4 then we are reducing our processing time by approximately 75%. This is approximate and greatly depends on the code, memory etc.
The point is not to get an exact number on how much we improved performance. We can measure that within our real world applications.The point is to show just how easy it is to write parallel sections of code. Dead simple right?
A few things to keep in mind. You should design your code so that each iteration of the loop is completely independent from the others. Any single iteration should not rely on another in order to complete correctly. If we do have shared variables that need to be accessed by each thread then we need to implement a locking scheme.
