In some simulation projects, a large number of computing problems are often encountered. It takes several hours to perform a simulation thousands of times. The user puts forward multiple requirements to improve the computing efficiency, we have also modified the computing logic multiple times to shorten the computing path, but still cannot achieve very obvious results.
During computing, I observed the CPU usage curve and found that the CPU usage is not much, and even the computing logic that occupies a high CPU only occupies a CPU kernel, so how can we make full use of the current mainstream multi-core CPU for computing?
. Net4.0 provides a new namespace: System. threading. tasks is used to provide related classes for Parallel Computing. Here I mainly introduce a simple class: Parallel, which is used to provide support for Parallel Loops and regions.
To put it simply, Parallel can convert a common for or foreach loop into a Parallel operation. Let's look at the Code:
Using System. Threading. Tasks;
Class Test
{
Static int N = 1000;
Static void TestMethod ()
{
// Using a named method.
Parallel. For (0, N, Method2 );
// Using an anonymous method.
Parallel. For (0, N, delegate (int I)
{
// Do Work.
});
// Using a lambda expression.
Parallel. For (0, N, I =>
{
// Do Work.
});
}
Static void Method2 (int I)
{
// Do work.
}
} It's amazing to make a small change so that the original for or foreach code can be executed in parallel ~
Next I made a small experiment. How much efficiency can a common for and Parallel. For be less efficient?
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. Diagnostics;
Using System. Threading. Tasks;
Using System. Threading;
Namespace ParallelLoopDemo
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void btnNormalLoop_Click (object sender, EventArgs e)
{
Int time = 0;
Int. TryParse (textBox1.Text, out time );
Stopwatch sw = new Stopwatch ();
Sw. Start ();
For (int I = 0; I <= time; I ++)
{
Calc (I );
}
Sw. Stop ();
Label2.Text = sw. ElapsedMilliseconds. ToString ();
}
Private void btnParallelLoop_Click (object sender, EventArgs e)
{
Int time = 0;
Int. TryParse (textBox1.Text, out time );
Stopwatch sw = new Stopwatch ();
Sw. Start ();
Parallel. For (0, time + 1, I =>
{
Calc (I );
});
Sw. Stop ();
Label2.Text = sw. ElapsedMilliseconds. ToString ();
}
Private void Calc (int time)
{
Math. Pow (time, time + 1 );
}
}
} Compare the running efficiency of the two in 10000 times respectively.
This figure shows the CPU usage of a regular for loop.
The following figure shows the CPU usage of Parallel..
Obviously, we can see that Parallel. For can make good use of multi-core CPU. If there is a 4-core CPU, the efficiency should be 4 times. I don't know which one can help the test.
It is very cost-effective to simply change the code to double the efficiency, but note the following: Parallel. for uses parallel operations, you need to consider the pre-conditions of the operation and the issues that need to be considered, such as mutual locks and interface refreshing.