Parallel.For Parallel Algorithms

Posted by kevin777 on Mon, 20 May 2019 20:28:28 +0200

Before I saw the use of Parallel, I felt very deep, very tired. Today I took time to study it, and found that it was still very easy.

NET Framework 4.0 added features, so before 4.0 can not be used oh.

Now, Parallel is called parallel algorithm. In vernacular, it is to make full use of the advantages of computer multi-core processors, so that every core can work hard, not let them idle, to improve operational efficiency.

However, there are several points to be paid attention to when using it:

1: When Parallel parallel processing involves sharing resources, use it carefully, because parallel access to shared resources at the same time, there will be an uncertain state, if not to use, it can be solved by locking.

2: In Parallel, whether For or Foreach, processing is disorderly, not in order, so be careful;

3: Parallel is not recommended if there are fewer lists or loops, because processing such as creating thread resources wastes a lot of events and resources.

4: Similarly, if the internal processing logic is very simple, it is not recommended to use Parallel, because the cost of creating resources is not worthwhile. For more complex processing logic, Parallel can be considered.

 

talk is cheap,show you code

        public static void parallelFunc()
        {
            DateTime startTime;
            TimeSpan resultTime;
            List<int> testList = new List<int>();
            for (int i = 0; i < 10; i++)
            {
                testList.Add(i);
            }
            startTime = System.DateTime.Now;
            loop1(testList);
            resultTime = System.DateTime.Now - startTime;
            Console.WriteLine("commonly for Cycle time:" + resultTime.TotalMilliseconds);
            startTime = System.DateTime.Now;
            loop2(testList);
            resultTime = System.DateTime.Now - startTime;
            Console.WriteLine("commonly foreach Cycle time:" + resultTime.TotalMilliseconds);
            startTime = System.DateTime.Now;
            loop3(testList);
            resultTime = System.DateTime.Now - startTime;
            Console.WriteLine("parallel for Cycle time:" + resultTime.TotalMilliseconds);
            startTime = System.DateTime.Now;
            loop4(testList);
            resultTime = System.DateTime.Now - startTime;
            Console.WriteLine("parallel foreach Cycle time:" + resultTime.TotalMilliseconds);
            Console.ReadLine();
        }

        #region  Parallel loop
        //Ordinary for loop
        static void loop1(List<int> source)
        {
            int count = source.Count();
            for (int i = 0; i < count; i++)
            {
                System.Threading.Thread.Sleep(100);
            }
        }

        //Ordinary foreach loop
        static void loop2(List<int> source)
        {
            foreach (int item in source)
            {
                System.Threading.Thread.Sleep(100);
            }
        }

        //Parallel for loop
        static void loop3(List<int> source)
        {
            int count = source.Count();
            Parallel.For(0, count, index =>
            {
                Console.WriteLine($"Parallel.For: {index}");
                System.Threading.Thread.Sleep(100);
            });
        }

        //Parallel foreach loop
        static void loop4(List<int> source)
        {
            Parallel.ForEach(source, item =>
            {
                Console.WriteLine($"Parallel.ForEach: {item}");
                System.Threading.Thread.Sleep(100);
            });
        }

        #endregion
    }

 

Operation results:

 

As you can see, the execution efficiency is Parallel. ForEach > Parallel. For > Ordinary ForEach > Ordinary For, and Parallel runs in disorder.

Specific efficiency should depend on the number of CPU cores in the computer. My CPU core is 4 cores, so it's about 4 times higher.

Topics: C#