I talked with others about the performance of LINQ yesterday. I feel that the performance of LINQ is not good, but in fact it is still outstanding in terms of performance, except for the ability to write data to SQL, the following is a simple performance test comparison code. Here I will explain that the execution speed of the Code cannot be tested using datetime. I am still a little white, just starting with datetime, the results showed that the performance of the LINQ service was not good, but stopwatch was used to discover the fact. Those who used to be biased against the LINQ service still had to embrace the LINQ service. Maybe some aspects were not fully understood yet, also ask the school friends to criticize and correct them.
Class Program
{
Static void main (string [] ARGs)
{
Test ();
}
Static void test ()
{
List <myclass> list1 = new list <myclass> ();
For (INT I = 0; I <10000000; I ++)
{
Myclass AA = new myclass ();
AA. Name = "Test Data" + I;
AA. ID = I;
List1.add (AA );
}
Stopwatch timer = new stopwatch ();
# Region for Loop
Timer. Start ();
List <myclass> list2 = new list <myclass> ();
Foreach (myclass s in list1)
{
If (S. ID >=52 & S. ID <850) {list2.add (s );}
}
Timer. Stop ();
Console. Write ("number of set matches" + list2.count + ", for loop time consumption :");
Console. writeline (timer. elapsed. ticks );
# Endregion
# Region LINQ
Timer = new stopwatch ();
Timer. Start ();
VaR list3 = list1.where (Product => product. ID >=52 & product. ID <850 );
Timer. Stop ();
Console. Write ("number of set matches" + list3.count () + ", LINQ time consumption :");
Console. writeline (timer. elapsed. ticks );
# Endregion
# Region delegate
Timer = new stopwatch ();
Timer. Start ();
List <myclass> list4 = list1.findall (delegate (myclass post)
{
Return post. ID> = 52 & post. ID <850;
});
Timer. Stop ();
Console. Write ("number of set matches" + list4.count () + ", delegate time consumption :");
Console. writeline (timer. elapsed. ticks );
# Endregion
Console. Read ();
}
Public class myclass
{
Public string name {Get; set ;}
Public int ID {Get; set ;}
}
}
The test results are as follows:
If you do not understand the correct information, please criticize it and avoid mistaken ideas.
According to yuanyou's criticism and correction, I reorganized the code. Although it is a simple thing, I am more grateful for the newbie's criticism than the installation.
The Code is as follows:
View code
Class Program
{
Static void main (string [] ARGs)
{
Test ();
}
Static void test ()
{
List <myclass> list1 = new list <myclass> ();
For (INT I = 0; I <10000000; I ++)
{
Myclass AA = new myclass ();
AA. Name = "Test Data" + I;
AA. ID = I;
List1.add (AA );
}
Stopwatch timer = new stopwatch ();
# Region for Loop
Timer. Start ();
List <myclass> list2 = new list <myclass> ();
Int COUNT = 0;
Foreach (myclass s in list1)
{
If (S. ID >=52 & S. ID <850) {list2.add (s );}
}
Count = list2.count;
Timer. Stop ();
Console. Write ("number of set matches" + Count + ", for loop time consumption :");
Console. writeline (timer. elapsed. ticks );
# Endregion
# Region LINQ
Timer = new stopwatch ();
Timer. Start ();
VaR list3 = list1.where (Product => product. ID >=52 & product. ID <850 );
// List1.where (Product => product. ID> = 52 & product. ID <850). toarray (). Count ();
Int count3 = list3.count ();
Timer. Stop ();
Console. Write ("number of set matches" + count3 + ", LINQ time consumption :");
Console. writeline (timer. elapsed. ticks );
# Endregion
# Region delegate
Timer = new stopwatch ();
Timer. Start ();
List <myclass> list4 = list1.findall (delegate (myclass post)
{
Return post. ID> = 52 & post. ID <850;
});
Int count4 = list4.count ();
Timer. Stop ();
Console. Write ("number of set matches" + count4 + ", time consumed by Delegate :");
Console. writeline (timer. elapsed. ticks );
# Endregion
Console. Read ();
}
Public class myclass
{
Public string name {Get; set ;}
Public int ID {Get; set ;}
}
}
Test
The third update code, this time there is no sequential problem:
View code
Class Program
{
Static void main (string [] ARGs)
{
List <myclass> list1 = new list <myclass> ();
For (INT I = 0; I <10000000; I ++)
{
Myclass AA = new myclass ();
AA. Name = "Test Data" + I;
AA. ID = I;
List1.add (AA );
}
Test_linq (list1 );
Test_foreach (list1 );
Test_delegate (list1 );
Console. Read ();
}
Static void test_foreach (list <myclass> list1)
{
Stopwatch timer = new stopwatch ();
# Region foreach Loop
Timer. Start ();
List <myclass> list2 = new list <myclass> ();
Int COUNT = 0;
Foreach (myclass s in list1)
{
If (S. ID >=52 & S. ID <850) {list2.add (s );}
}
Count = list2.count;
Timer. Stop ();
Console. Write ("number of set matches" + Count + ", for loop time consumption :");
Console. writeline (timer. elapsed. ticks );
# Endregion
}
Static void test_linq (list <myclass> list1)
{
Stopwatch timer = new stopwatch ();
# Region LINQ
Timer = new stopwatch ();
Timer. Start ();
VaR list3 = list1.where (Product => product. ID >=52 & product. ID <850 );
// List1.where (Product => product. ID> = 52 & product. ID <850). toarray (). Count ();
Int count3 = list3.count ();
Timer. Stop ();
Console. Write ("number of set matches" + count3 + ", LINQ time consumption :");
Console. writeline (timer. elapsed. ticks );
# Endregion
}
Static void test_delegate (list <myclass> list1)
{
Stopwatch timer = new stopwatch ();
# Region delegate
Timer = new stopwatch ();
Timer. Start ();
List <myclass> list4 = list1.findall (delegate (myclass post)
{
Return post. ID> = 52 & post. ID <850;
});
Int count4 = list4.count ();
Timer. Stop ();
Console. Write ("number of set matches" + count4 + ", time consumed by Delegate :");
Console. writeline (timer. elapsed. ticks );
# Endregion
}
Public class myclass
{
Public string name {Get; set ;}
Public int ID {Get; set ;}
}
}
It seems that there are basically no major changes.
Conclusion: I do not know the processing of the slow Loading Function of LINQ. Therefore, the Count operation of LINQ triggers the actual query operation of LINQ, which may not be fully understood yet, but will be understood one day.
Blog: http://www.jqpress.com/post/43.aspx