Monday, November 24, 2008

Can Parallel Programming Improve Performance

I love to optimize my code for performance, whenever I can.
So I wrote a simple speed test to see what works best.

I've use the new System.Threading, provided in the Parallel Extensions June 08 CTP from Microsoft and I have to said it is bloody awesome.

For example a simple for / foreach loop:

for (var item = 0; item < list.Length; item++)
{
DoSomeWork();
}

foreach (var item in list)
{
DoSomeWork();
}


This is the same example, but using a parallel for / foreach loop:
Parallel.For(0, list.Length, item => { DoSomeWork(); });

Parallel.ForEach(list, DoSomeWork);

Coding it is as easy as pie, it makes use of all the cores that your CPU has to offer.

Speed Test

Please let me know if this helps you.

No comments: