Parallel. For parallel algorithm, Parallel. for Algorithm
I have seen the usage of Parallel before, and I think it is very advanced and difficult. Today I have made some research and found that it is still very easy.
. NET Framework 4.0 is newly added, so it cannot be used before 4.0.
Parallel is called a Parallel algorithm. In other words, Parallel aims to make full use of the advantages of the multi-core computer processor, so that every core can work hard to keep them idle and improve operation efficiency.
However, pay attention to the following points:
1: When Parallel processing involves sharing resources, you should be very careful when using it. Because Parallel access to shared resources at the same time, there will be uncertain States. If you have to use it, you can lock it to solve it;
2: In Parallel, whether it is For or Foreach, the processing is out of order, not in order, so be careful;
3: If the list or loop times are small, Parallel is not recommended because processing such as creating thread resources will waste a lot of events and resources;
4: Similarly, if the internal processing logic is very simple, Parallel is not recommended because it is not worthwhile to create resources. For more complex processing logic, consider Parallel.
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 ("General for loop time:" + resultTime. totalMilliseconds); startTime = System. dateTime. now; loop2 (testList); resultTime = System. dateTime. now-startTime; Console. writeLine ("General foreach cycle time:" + resultTime. totalMilliseconds); startTime = System. dateTime. now; loop3 (testList); resultTime = System. dateTime. now-startTime; Console. writeLine ("parallel for loop time consumption:" + resultTime. totalMilliseconds); startTime = System. dateTime. now; loop4 (testList); resultTime = System. dateTime. now-startTime; Console. writeLine ("parallel foreach loop time:" + resultTime. totalMilliseconds); Console. readLine () ;}# region Parallel loop // common for loop static void loop1 (List <int> source) {int count = source. count (); for (int I = 0; I <count; I ++) {System. threading. thread. sleep (100) ;}/// General 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}
View Code
Running result:
We can see that the execution efficiency is: Parallel. ForEach> Parallel. For> normal ForEach> normal For, and Parallel runs unordered.
The specific efficiency depends on the number of CPU cores on the computer. I have 4 cores, so it is about 4 times.