How to maximize the improvement. NET's performance

Source: Internet
Author: User
Tags comparison error handling variables
Performance

Optimization. NET's performance

1 Avoid using ArrayList.
Because any object added to the ArrayList must be sealed to the System.Object type, remove the data from the ArrayList, to remove the actual type back. It is recommended that you use a custom collection type instead of ArrayList. NET 2.0 to provide a new type, called Generics, which is a strong type, and using generic collections avoids the occurrence of enclosures and unboxing and improves performance.

2 use Hashtale instead of other dictionary collection types (such as stringdictionary,namevaluecollection,hybridcollection), when storing small amounts of data, you can use Hashtable.

3 Declare a constant for the string container, and do not encapsulate the character directly inside the double quote "".
Avoid
//
MyObject obj = new MyObject ();
Obj. Status = "ACTIVE";

Recommended
Const string c_status = "ACTIVE";
MyObject obj = new MyObject ();
Obj. Status = C_status;

4 Do not use the Uppercase,lowercase conversion string for comparison, with String.Compare instead, it can ignore the case for comparison.

Cases:

Const string c_value = "COMPARE";
if (String.Compare (svariable, C_value, true) = = 0)
{
Console.Write ("SAME");
}


5 use StringBuilder instead of using string connector "+".

Avoid
String SXML = "<parent>";
SXML = "<child>";
SXML + + + "Data";
SXML = "</child>";
SXML = "</parent>";

Recommended
StringBuilder sbxml = new StringBuilder ();
Sbxml.append ("<parent>");
Sbxml.append ("<child>");
Sbxml.append ("Data");
Sbxml.append ("</child>");
Sbxml.append ("</parent>");

6 If You are only reading from the XML object, avoid using Xmldocumentt, instead use XPathDocument, which is readonly and So improves performance.
If you simply read data from an XML object and replace XmlDocument with a read-only XPathDocument, you can improve performance
Avoid
XmlDocument xmld = new XmlDocument ();
Xmld. Loadxml (SXML);
txtName.Text = Xmld. selectSingleNode ("/packet/child"). InnerText;

.

Recommended
XPathDocument Xmldcontext = new XPathDocument (new StringReader (Ocontext.value));
XPathNavigator Xnav = Xmldcontext.createnavigator ();
XPathNodeIterator xpnodeiter = Xnav. Select ("Packet/child");
icount = Xpnodeiter.count;
Xpnodeiter = Xnav. Selectdescendants (XPathNodeType.Element, false);
while (Xpnodeiter.movenext ())
{
Scurrvalues + + xpnodeiter.current.value+ "~";
}


7 Avoid declaring variables in the loop body, declare variables outside the loop body and initialize them in the loop body.

Avoid
for (int i=0; i<10; i++)
{
SomeClass OBJSC = new SomeClass ();
.
.
.

}

Recommended
SomeClass OBJSC = null;
for (int i=0; i<10; i++)
{
OBJSC = new SomeClass ();

.
.
.
}

8 Capture the specified exception and do not use the generic System.Exception.

Avoid
Try
{
<some logic>
}
catch (Exception exc)
{
<error handling>
}

Recommended
Try
{
<some logic>
}
catch (System.NullReferenceException exc)
{
<error handling>
}
catch (System.ArgumentOutOfRangeException exc)
{
<error handling>
}
catch (System.InvalidCastException exc)
{
<error handling>
}

9 when using try...catch...finally, it is necessary to release the occupied resources such as connection, file flow and so on in finally
Otherwise, the resource occupied after the catch to the error cannot be freed.

Try
{
...
}
Catch
{...}
Finally
{
Conntion.close ()
}
10 avoid the use of recursive calls and nested loops, which can severely affect performance and are used when they have to be used.

11 Use appropriate caching strategy to improve performance
Well, write it here today, and then write it later.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.