0

I'm reading about parallel for (and foreach) and run code like this:

var ints = new int[50000];
Random rnd = new Random();
for (int i = 0; i < ints.Length; i++)
    ints[i] = rnd.Next(1000, 9999);

int sum = 1;
Stopwatch stopper = new Stopwatch();

stopper.Start();
for (int i = 0; i < ints.Length; i++)
{
    for (int j = 0; j < ints.Length; j++)
        sum++;
    sum = sum * ints[i];
}

stopper.Stop();
var ordinary = stopper.Elapsed;

stopper = new Stopwatch();

int sum2 = 1;
stopper.Start();
Parallel.For(0, ints.Length, i =>
{
    for (int j = 0; j < ints.Length; j++)
        sum2++;
});
stopper.Stop();
var paralel = stopper.Elapsed;

Console.WriteLine("ordinary\t{0}\nparalel\t{1}", ordinary, paralel);
Console.WriteLine("sum1:{0}\tsum2{1}", sum, sum2);

But the result isn't clear for me:

ordinary        00:00:04.9762059
paralel 00:00:23.2902043
sum1:1911321936 sum2474452665

Can you tell me why in this case parallel is slower than ordinary for?

I've read this topic Parallel.ForEach Slower than ForEach

But aren't there enough to count and parallel.for shouldn't be faster in this case?

Can you give me some example where i can see parallel.for is really faster?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
SharkyShark
  • 370
  • 1
  • 3
  • 15
  • 1
    From the link you pointed to `Parallelization is not free. The cost of splitting up work amongst different threads needs to be tiny compared to the amount of work done per thread.` The work you are having it do is easy (fast). Creating of threads is expensive (slow). Thus it is slower overall. – mjwills Jan 03 '18 at 07:15
  • 3
    Your parallel code is also not thread-safe (i.e. doesn't work) either. `sum2++;` is not safe to run from multiple threads simultaneously. – mjwills Jan 03 '18 at 07:16
  • 1
    Anyone who needs an example provided hasn't thought it through. You can simulate work of any length with a call to `Thread.Sleep`. – jmcilhinney Jan 03 '18 at 07:28
  • 1
    `can you give me some example where i can see paralel.for is really faster` Where the work is large enough to outweigh the threading cost. https://blogs.msdn.microsoft.com/pfxteam/2009/06/06/achieving-speedups-with-small-parallel-loop-bodies/ https://www.google.com.au/search?q=when+to+use+parallel+for&oq=when+to+use+parallel+for – mjwills Jan 03 '18 at 07:29
  • jmcilhinney - yeah i know - i have used some async methods when was working with crud's. i just wanted some nice example on which i see paralel.for is faster. thanks mjwills – SharkyShark Jan 03 '18 at 07:40
  • As far as I'm concerned, the whole example is completely useless as long as it produces different results. Performance comparison should only happen on code with comparable results. – grek40 Jan 03 '18 at 08:27
  • You should start learning by reading the [documentation](https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-simple-parallel-for-loop) they have a good example that runs much faster on a muti-core machine. – Filip Cordas Jan 03 '18 at 09:46
  • Also in this example my guess that the compiler dose some weird optimization and that is why the non parallel loop runs faster. Performance can only be tested on your real world example the results can also wary between Xeon and I7 processors so be carful with tests like this. – Filip Cordas Jan 03 '18 at 09:54

0 Answers0