Parallel.For vs vs for

I have Parallel.For and a regular loop to do simple arithmetic, just to compare Parallel.For

I came to the conclusion that regular startup on my i5 laptop is faster.

This is my code.

using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int Iterations = int.MaxValue / 1000; DateTime StartTime = DateTime.MinValue; DateTime EndTime = DateTime.MinValue; StartTime = DateTime.Now; Parallel.For(0, Iterations, i => { OperationDoWork(i); }); EndTime = DateTime.Now; Console.WriteLine(EndTime.Subtract(StartTime).ToString()); StartTime = DateTime.Now; for (int i = 0; i < Iterations; i++) { OperationDoWork(i); } EndTime = DateTime.Now; Console.WriteLine(EndTime.Subtract(StartTime).ToString()); StartTime = DateTime.Now; Parallel.For(0, Iterations, i => { OperationDoWork(i); }); EndTime = DateTime.Now; Console.WriteLine(EndTime.Subtract(StartTime).ToString()); StartTime = DateTime.Now; for (int i = 0; i < Iterations; i++) { OperationDoWork(i); } EndTime = DateTime.Now; Console.WriteLine(EndTime.Subtract(StartTime).ToString()); } private static void OperationDoWork(int i) { int a = 0; a += i; i = a; a *= 2; a = a * a; a = i; } } } 

And these are my results. Which doesn’t change much for repetition:

 00:00:03.9062234 00:00:01.7971028 00:00:03.2231844 00:00:01.7781017 

So why use Parallel.For?

+4
source share
2 answers

One of the most common mistakes that occurs when you first become aware of multithreading is the belief that multithreading is Free Lunch.

In truth, dividing your operation into several smaller operations, which can then be done in parallel, will take some extra time. If you are poorly synchronized, your tasks may spend even more time waiting for other tasks to release their locks.

As a result; parallelization is not worth the time / problems, when each task will work little, which is the case with OperationDoWork .

Edit:

Think about it:

  private static void OperationDoWork(int i) { double a = 101.1D * i; for (int k = 0; k < 100; k++) a = Math.Pow(a, a); } 

According to my test, for will be on average up to 5.7 seconds, and Parallel.For will take 3.05 seconds on my Core2Duo processor (speedup == ~ 1.87).
On my Quadcore i7, I get an average of 5.1 seconds with for and an average of 1.38 seconds with Parallel.For (speedup == ~ 3.7).

This modified code scales very well to the number of available physical cores. QED

+7
source

Parallel processing has an organization overhead. Think about it, bearing in mind 100 tasks and 10 people. It’s not easy for 10 people to work for you, simply organizing who does what is worth the time and also performs 100 tasks.

So, if you want to do something in parallel, make sure that there is so much work that the workload of the parallelism organization is so small compared to the actual workload that it makes sense to do it.

+9
source

Source: https://habr.com/ru/post/1483773/


All Articles