I created a simple test with big arrays, and I get a big difference between parallel for and normal for
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var rn = new Random(541);
var tmStart = DateTime.Now;
int[,] a = new int[2048, 2048];
for (int i = 0; i < 2048; i++)
for (int j = 0; j < 2048; j++)
a[i, j] = rn.Next();
var tmEnd = DateTime.Now - tmStart;
Console.WriteLine("Normal..: {0}",tmEnd.ToString());
tmStart = DateTime.Now;
a = new int[2048, 2048];
Parallel.For(0, 2048, i =>
{
for (int j = 0; j < 2048; j++)
a[i, j] = rn.Next();
});
tmEnd = DateTime.Now - tmStart;
Console.WriteLine("Parallel: {0}", tmEnd.ToString());
}
}
}
The time for process:
Normal..: 00:00:00.1250071
Parallel: 00:00:00.3880222
Why is that difference so big?
I imagined that if you use several threads, it would be faster...