5
votes

I've been learning a little about parallelism in the last few days, and I came across this example.

I put it side to side with a sequential for loop like this:

private static void NoParallelTest()
{
    int[] nums = Enumerable.Range(0, 1000000).ToArray();
    long total = 0;
    var watch = Stopwatch.StartNew();
    for (int i = 0; i < nums.Length; i++)
    {
        total += nums[i];
    }
    Console.WriteLine("NoParallel");
    Console.WriteLine(watch.ElapsedMilliseconds);
    Console.WriteLine("The total is {0}", total);
}

I was surprised to see that the NoParallel method finished way way faster than the parallel example given at the site.

I have an i5 PC.

I really thought that the Parallel method would finish faster.

Is there a reasonable explanation for this? Maybe I misunderstood something?

2
Can you confirm that the parallel version actually ran on multiple cores? And what happens when you increase the number of iterations (larger Range)?chrisaycock
Assuming the parallel version did run on multiple cores, it can simply show you how much overhead thread synchronization can have... especially in a trivial piece of code.Oded
To paraphrase Mark Twain; "There are lies, damn lies, statistics and benchmarks ..."user177800

2 Answers

12
votes

The sequential version was faster because the time spent doing operations on each iteration in your example is very small and there is a fairly significant overhead involved with creating and managing multiple threads.

Parallel programming only increases efficiency when each iteration is sufficiently expensive in terms of processor time.

2
votes

I think that's because the loop performs a very simple, very fast operation.

In the case of the non-parallel version that's all it does. But the parallel version has to invoke a delegate. Invoking a delegate is quite fast and usually you don't have to worry how often you do that. But in this extreme case, it's what makes the difference. I can easily imagine that invoking a delegate will be, say, ten times slower (or more, I have no idea what the exact ratio is) than adding a number from an array.