Recently I've played with parallel loops. I started with simple tasks as it is filling in a huge array.
However, the creation time was half of second when the code was NOT parallel and 6.03s (sic!) when the code WAS parallel.
How come?
I thought there is no simpler task to show benefits from parallelism as dividing huge task to smaller one, as I did.
Could some one explain?
12GB RAM, i7 extreme 980 (6 cores + 6 virtual) 3.06G
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ParallelLoop
{
class Program
{
static void Main(string[] args)
{
int Min = 0;
int Max = 10;
int ArrSize = 150000000;
Stopwatch sw2 = new Stopwatch();
Stopwatch sw3 = new Stopwatch();
int[] test2 = new int[ArrSize];
int[] test3 = new int[ArrSize];
Random randNum = new Random();
sw2.Start();
for (int i = 0; i < test2.Length; i++)
{
test2[i] = i;
//test2[i] = randNum.Next(Min, Max);
}
sw2.Stop();
Console.ReadKey();
Console.WriteLine("Elapsed={0}", sw2.Elapsed);
sw3.Start();
Parallel.For(0, test3.Length, (j) =>
{
test3[j] = j;
//test3[j] = randNum.Next(Min, Max);
}
);
sw3.Stop();
Console.WriteLine("Elapsed={0}", sw3.Elapsed);
Console.ReadKey();
}
}
}