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