Optimize .NET performance
1) Avoid using ArrayList.
Because any object added to the ArrayList must be boxed as System.Object type, when data is retrieved from the ArrayList, it must be unboxed back to the actual type. It is recommended to use a custom collection type instead of ArrayList. .net 2.0 provides a new type called generics, which is a strong type. Using generic collections can avoid boxing and unboxing and improve performance.
2) Use HashTale instead of other dictionary collection types (such as StringDictionary, NameValueCollection, HybridCollection). HashTable can be used when storing a small amount of data.
3) Declare constants for string containers, and do not directly encapsulate characters in double quotes " ".
//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 UpperCase or Lowercase to convert strings for comparison. Use String.Compare instead, which can ignore case for comparison.
example:
const string C_VALUE = "COMPARE";
if (String.Compare(sVariable, C_VALUE, true) == 0)
{
Console.Write("SAME");
}
5) Use StringBuilder instead of using the string concatenation character "+", .
//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 only read data from XML objects, use read-only XPathDocument instead of XMLDocument to 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 and initialize them in the loop body.
//avoid
for(int i=0; i<10; i++)
{
SomeClass objSC = new SomeClass();
.
.
.
}
//recommend
SomeClass objSC = null;
for(int i=0; i<10; i++)
{
objSC = new SomeClass();
.
.
.
}
8) Catch the specified exception, do not use the general System.Exception.
//Avoid
try
{
<some logic>
}
catch(Exception exc)
{
<Error handling>
}
//recommend
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, the occupied resources such as connections, file streams, etc. must be released in finally. Otherwise, the occupied resources cannot be released after an error is caught in Catch.
try
{
...
}
catch
{...}
finally
{
connection.close()
}
10) Avoid using recursive calls and nested loops. Using them will seriously affect performance. Use them only when they have to be used.
11) Use appropriate caching strategies to improve performance. That’s it for today. I’ll write more when I have time.
http://www.cnblogs.com/timone/archive/2006/11/17/563965.html