New on framework 4 - Parallel

Not long ago, I started to learn what's new on framework 4 (Not that it was in sight in the SharePoint - still stuck at 3.5)

Anyway, I took a sample from "Love the Dot"
There is a new concept in the framework 4 - Parallel
Parallel gives us to use the computer's CPU resources
They are a small example - I will present for two loops of
The first is the Parallel method
Parallel.For (0, 250, (i) =>
Second - we know from ancient man ovalness
for (int i = 0; i <250; i + +)

Here's the code

using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            Parallel.For(0, 250, (i) =>
            {
                RandomMix(i);
            }
            );
            watch.Stop();
            Console.WriteLine(String.Format("Parallel.For tooks {0} milliseconds", watch.ElapsedMilliseconds));
            watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < 250; i++)
            {
                RandomMix(i);
            }
            watch.Stop();
            Console.WriteLine(String.Format("for tooks {0} milliseconds", watch.ElapsedMilliseconds));
            Console.ReadLine();
        }
        private static void RandomMix(int instance) 
        { 
            double result = Math.Acos(new Random().NextDouble()) * Math.Atan2(new Random().NextDouble(), new Random().NextDouble());  
            for (int i = 0; i < 20000; i++)  
            { 
                result += (Math.Cos(new Random().NextDouble()) * Math.Acos(new Random().NextDouble())); 
            }  
        }
    }
}


This result (notice the picture with multiple processors)

Parallel.For tooks 2.6 sec
for tooks 22.1 sec

Amazing - it really works
For those interested there is also continuing on - Parallel LINQ (PLINQ)

Comments

Popular posts from this blog

A sharepoint list view of the current month

The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters

Export SharePoint 2010 List to Excel with PowerShell