so as said in the tile I'm trying to fill up an array of bytes with random numbers using 16 (in my case) threads, now it takes about six and a half seconds filling up an array with 500000000 bytes using one thread so the logic says that using 16 threads will be at least 10 times faster but then I tried to do this, it took 15 seconds to fill it up, what I did is I gave each thread one segment to fill in the same array
here is the code:
static byte[] nums2 = new byte[500000000];
static Random rnd = new Random(123);
static void fill()
{
for (int i = 0; i < nums.Length; i++)
nums[i] = (byte)rnd.Next(10);
}
static void fillPart(object ID)
{
var part = nums2.Length / Environment.ProcessorCount;
int baseN = (int)ID * part;
for (int i = baseN; i < baseN + part; i++)
nums2[i] = (byte)rnd.Next(10);
Console.WriteLine("Done! " + ID);
}
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Start();
fill();
watch.Stop();
Console.WriteLine("it took " + watch.Elapsed);
Console.WriteLine();
watch.Reset();
watch.Start();
Thread[] threads = new Thread[Environment.ProcessorCount];
for (int i = 0; i < Environment.ProcessorCount; i++)
{
threads[i] = new Thread(fillPart);
threads[i].Start(i);
}
for(int i = 0; i < Environment.ProcessorCount; i++)
threads[i].Join();
watch.Stop();
Console.WriteLine("it took " + watch.Elapsed);
}
}```
would like to understand why is it took 15 seconds or maybe what I did wrong
Aucun commentaire:
Enregistrer un commentaire