Problem | performance
1, StringBuilder and string (great difference)
string s = "";
Long T1 = System.DateTime.Now.Ticks;
for (int i=0;i<10000;i++) s + = convert.tostring (i);
Long t2 = System.DateTime.Now.Ticks;
long t3 = System.DateTime.Now.Ticks;
System.Text.StringBuilder sb = new System.Text.StringBuilder ();
for (int i=0;i<10000;i++) sb. Append (Convert.ToString (i));
string S1 = sb. ToString ();
long T4 = System.DateTime.Now.Ticks;
Long T5 = System.DateTime.Now.Ticks;
System.Text.StringBuilder sb1 = new System.Text.StringBuilder (5000);
for (int i=0;i<10000;i++) sb1. Append (i);
String s2 = sb1. ToString ();
Long T6 = System.DateTime.Now.Ticks;
Long t7 = System.DateTime.Now.Ticks;
System.Text.StringBuilder SB2 = new System.Text.StringBuilder (10000);
for (int i=0;i<10000;i++) SB2. Append (i);
String s3 = Sb2. ToString ();
Long T8 = System.DateTime.Now.Ticks;
ListViewItem item1 = result. Items.Add ("Use string class, use operator + to operate");
Item1. SubItems.Add (convert.tostring (T2-T1)/10000));
ListViewItem item2 = result. Items.Add ("Use StringBuilder class, do not assign value to initial size");
Item2. SubItems.Add (convert.tostring (T4-T3)/10000));
ListViewItem Item3 = result. Items.Add ("Use the StringBuilder class, assign the initial size to 1/2 of the number of applications");
Item3. SubItems.Add (convert.tostring (T6-T5)/10000));
ListViewItem ITEM4 = result. Items.Add ("Use the StringBuilder class, assign the initial size to the number of requests");
Item4. SubItems.Add (convert.tostring (T8-T7)/10000));
2, the impact of the performance of the exception, of course, the following example did not carry out abnormal distribution, capture, etc., so the time is not long.
int j = 0;
Long T1 = System.DateTime.Now.Ticks;
for (int i = 0; i < 10000; i++)
{
Try
{
j = i;
throw new System.Exception ();
}
Catch {}
}
Long t2 = System.DateTime.Now.Ticks;
long t3 = System.DateTime.Now.Ticks;
for (int i = 0; i < 10000; i++)
{
Try
{
j = i;
throw new System.Exception ();
}
Catch {}
}
long T4 = System.DateTime.Now.Ticks;
Result. Items.clear ();
ListViewItem item1 = result. Items.Add ("Throw 10,000 exceptions");
Item1. SubItems.Add (convert.tostring (T2-T1)/10000));
ListViewItem item2 = result. Items.Add ("shielding anomaly");
Item2. SubItems.Add (convert.tostring (T4-T3)/10000));
3. Use foreach Loop and use for loop (big difference)
string s = "monkeys!";
int dummy = 0;
System.Text.StringBuilder sb = new System.Text.StringBuilder (s);
for (int i = 0; i < 1000000 i++) sb. Append (s);
s = sb. ToString ();
Long T1 = System.DateTime.Now.Ticks;
foreach (char c in s) dummy++;
Long t2 = System.DateTime.Now.Ticks;
dummy = 0;
long t3 = System.DateTime.Now.Ticks;
for (int i = 0; i < 1000000; i++) dummy++;
long T4 = System.DateTime.Now.Ticks;
Result. Items.clear ();
ListViewItem item1 = result. Items.Add ("Use foreach Loop");
Item1. SubItems.Add (convert.tostring (T2-T1)/10000));
ListViewItem item2 = result. Items.Add ("Use for loop");
Item2. SubItems.Add (convert.tostring (T4-T3)/10000));
4, Latebinding and DirectCall comparison (the difference is also relatively large)
Long T1 = System.DateTime.Now.Ticks;
Latebindingtest LBT = new Latebindingtest ();
for (int i=0;i<100000;i++)
{
string s = lbt. Getsomewhat ();
}
Long t2 = System.DateTime.Now.Ticks;
long t3 = System.DateTime.Now.Ticks;
Type t = Type.GetType ("Perfdemo.latebindingtest");
MethodInfo mi = t.getmethod ("Getsomewhat");
for (int i=0;i<100000;i++)
{
Object s2 = mi. Invoke (Lbt,null);
}
long T4 = System.DateTime.Now.Ticks;
Result. Items.clear ();
ListViewItem item1 = result. Items.Add ("method of calling class directly (microseconds)");
Item1. SubItems.Add (convert.tostring (T2-T1)/10));
ListViewItem item2 = result. Items.Add ("a method (microseconds) with a late-bound way class");
Item2. SubItems.Add (convert.tostring (T4-T3)/10));
5. This is the class definition used in the example above:
public class Latebindingtest
{
public string Getsomewhat ()
{
return "hello,fqq!";
}
}
6, we can combine taskmgr and performancemonitor to carry on the surveillance, the conclusion is more interesting