Uncle also said that parallel and serial performance is improved by N times (N is determined by the number of operating systems and the number of cpu cores), and the performance is improved by n
Returned directory
Parallelism is. net4.5 focuses on the technology and is encapsulated into System. threading. in the Tasks namespace, the static class Parallel is provided for external users. We can directly use its static method, which can be a delegate array in Parallel or an IEnumerable iteration, today, we mainly use a code to access the database, which means that the improvement of concurrent Parallel on the entire program is so great. We can clearly see the effect from the number of database connections and running time.
Simple test code
[TestMethod] public void Read() { Stopwatch sw = new Stopwatch(); sw.Start(); var actions = new List<Action>(); for (int i = 0; i < 1000; i++) { actions.Add(() => { using (var db = new am20160316Entities()) { var repository = new Lind.DDD.Repositories.EF.EFRepository<ad_contract>(db); var list = repository.GetModel().ToList(); } }); } Parallel.Invoke(actions.ToArray()); sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); }
The above code isParallel Execution [parallel execution of available thread Data](This does not mean that 1000 tasks must be enabled with 1000 threads, because. net itself is also the concept of thread pool, all it will be allocated to our process according to the system usage, if your various aspects of the request needs. net allocates more threads so that multiple threads can be started now (if it is connected to the database, it is related to the MaxPoolSize and MinPoolSize of your data connection string ).
Connection between programs and servers
When the program communicates with the database, some connections are established. We can use the netstat-nba | findstr 192.168.2.123 command to view the communication with the specified server. When we modify the MinPooSize, it will affect the number of times this program establishes communications with the database. Of course, the more times this program establishes communications at the same time, the better the performance. Of course, this also depends on the capabilities of the database server.
In fact, if our program is executed in a single thread in serial mode, as long as the figure above shows an active connection with a performance of over 10 connections at the same time, it is definitely incomparable, it is easy to understand that, just as if 0.1 million of the first people crossed the bridge, the time would be different if 0.1 million people passed the bridge or zhuqiao.
The above is the time for parallel processing of 1000 connections, 3427 milliseconds. For Single-thread serial, let's take a look at its processing time.
Code
[TestMethod] public void SignalRead() { Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 1000; i++) { using (var db = new am20160316Entities()) { var repository = new Lind.DDD.Repositories.EF.EFRepository<ad_contract>(db); var list = repository.GetModel().ToList(); } } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); }
Establish a single connection with the database
Running time
We can see from the above that the serial speed is more than 1000 milliseconds slower than the parallel speed, and this value will increase with the increase in concurrency.
Therefore, in the multi-core era, in the era of high operating system processing bits, we should try to use parallelism as much as possible!
Thank you for reading this article!
Returned directory