The 4.0 feature extends parallel task processing, which encapsulates threadpool tasks. The garden has a lot of Introduction to parallel's parallel computing. In fact, I prefer parallel query Plinq. People who have done complex SQL mosaic reports should have a deep understanding. When the execution of complicated SQL statements takes too long, how do you do it? Is the execution time after the stored procedure optimization unacceptable? The previous solution was to split complex SQL statements into several segments (possibly multiple tables) for parallel execution, load the query results to the memory, and obtain the intersection data using the LINQ processing, of course, this is achieved at the expense of memory, but if the queried data does not change much in the short term, this method is certainly a good solution to solve the slow query speed. On this basis, Plinq further expanded the results of the LINQ operation. The following is a Plinq million records query test.CodeAnd Result
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using Microsoft. Practices. Unity;
Using Ecepdi. Document. application. filemodule;
Using Ecepdi. Document. Infrastructure. unitofwork;
Using Ecepdi. Document. Infrastructure. repositories;
Using Ecepdi. Document. domain. entities;
Using System. Threading. tasks;
Using System. diagnostics;
Namespace Scanfileuploaddemo
{
Class Program
{
Static Void Main ( String [] ARGs)
{
Scanfilerepository processrepository = container. Current. Resolve ( Typeof (Scanfilerepository )) As Scanfilerepository;
Stopwatch Sw = New Stopwatch ();
Sw. Start ();
VaR Query = processrepository. getfiltered (O => O. filename. Contains (" F201502S-K0518 " ));
Console. writeline ( " Direct query results: " + Query. Count (). tostring ());
Sw. Stop ();
Console. writeline ( " When querying data directly: " + Sw. elapsedmilliseconds/1000d );
Sw. Reset ();
Sw. Start ();
List <scanfile> List = processrepository. getall (). tolist ();
Sw. Stop ();
Console. writeline ( " When loading data: " + Sw. elapsedmilliseconds/1000d );
Console. writeline ( " ------------------------------------------------- " );
Sw. Reset ();
Sw. Start ();
VaR Result =From U In List
Where U. filename. Contains ( " F201502S-K0518 " )
Select U;
Console. writeline ( " Single Row query results: " + Result. Count (). tostring ());
Sw. Stop ();
Console. writeline ( " Single Row query time: " + Sw. elapsedmilliseconds/1000d );
Console. writeline ( " ------------------------------------------------- " );
Sw. Reset ();
Sw. Start ();
VaR Result2 = From U In List. asparallel ()
Where U. filename. Contains ( " F201502S-K0518 " )
Select U;
Console. writeline ( " Parallel query results: " + Result2.count (). tostring ());
Sw. Stop ();
Console. writeline ( " Parallel query: " + Sw. elapsedmilliseconds/1000d );
Console. Readline ();
}
}
}
Usage:
The direct query result is to generate an SQL statement and query it in the database. The result is 5.453, while the database directly executes the same SQL statement, but it takes 7 seconds.
The query result of a single row in the LINQ memory is only 0.31 seconds, provided that the data must be loaded into the memory.
The parallel query result of Plinq memory is 0.114 seconds, which has obvious advantages. However, the Plinq parallel query results are unordered (disrupting the order of the original single row query results), because the completion time of each thread is inconsistent, as a result, the results merged after multiple threads are queried are unordered.
Plinq provides asordered for sorting the query results. Of course, this will lose some advantage.
Parallel. For/parallel. foreach is difficult to represent parallel advantages in current situations. Compared with parallel. invoke, there are many performance opportunities. The application performance efficiency of parallel queries and parallel tasks needs to be much higher than for/foreach.
Like tasks, parallel. Invoke is a good choice in parallel task processing.
Test1 ();
Test2 ();
Test3 ();
// Three things must be faster than one at the same time.
Parallel. Invoke (
() => Test1 (),
() => Test2 (),
() => Test3 ());
// Parallel. For/parallel. foreach cannot play too many advantages in normal traversal (the following situations are not as good as for/foreach)
Stopwatch Sw = New Stopwatch ();
Sw. Start ();
For (Int32 I = 0 ; I < 1000 ; I ++) dowork (I );
Sw. Stop ();
Console. writeline (SW. elapsedmilliseconds/1000d );
Sw. Reset ();
Sw. Start ();
Parallel. ( 0 , 1000 , I => dowork (I ));
Sw. Stop ();
Console. writeline (SW. elapsedmilliseconds/1000d );
VaR Collection = New Int32 [ 0 ];
Sw. Reset ();
Sw. Start ();
Foreach ( VaR Item In Collection) dowork (item );
Sw. Stop ();
Console. writeline (SW. elapsedmilliseconds/1000d );
Sw. Reset ();
Sw. Start ();
Parallel. foreach (collection, item => dowork (item ));
Sw. Stop ();
Console. writeline (SW. elapsedmilliseconds/1000d );
PS: complex SQL statements and stored procedure reports are clearly written, but you can't even understand them when you come back and change them later.-_-|.