Optimize .NET development with these performance tips
The .NET Framework class library is vast and sometimes overwhelming.
As with any development language there are numerous ways to solve a problem, but some methods are better than others. I'll examine a few areas where performance may be improved by using a set approach.
When to throw an exception
Exception handling is a key aspect of .NET development. It
allows you to easily and gracefully recover from errors that could easily crash
an application, but they can be expensive in terms of system resources. For
this reason, you should exercise restraint when using exceptions in your
applications.
Now, the number of try/catch blocks within your code does not indicate the number exceptions generated by the code. The catch block is only executed when an actual exception is encountered. Also, exceptions should not be used to control program flow. I've seen developers throw exceptions to abort a loop, like the following C# sample:
classExceptionTestClass {static void
try {
for (int x=0; x < 100; x++) {
if (x == 50) throw new Exception();
} }
catch {}
} }
Notice that the catch block does nothing so program execution continues. The proper way to address this situation is by using a break statement:
classExceptionTestClass {static void
try {
for (int x=0; x < 100; x++) {
if (x == 50) break;
}Â }
catch {}
} }
Here's the equivalent approach in VB.NET:
Sub Main()Dim x As Integer
Try
For x = 0 To 99
If (x = 50) Then
Exit For
End If
Next x
Catch
End Try
End Sub
Microsoft does include the perfmon tool that may be used to monitor the exceptions thrown by an application. It may surprise you to find that certain areas of your application throw more exceptions than you expected. For better granularity, you can also check the exception number programmatically by using Performance Counters.
Working with stringsWhen a string is altered, the original string is garbage collected with a new object created to hold the changed string. This may not be an issue for a small number of changes, but an excessive number can tax the system.
As with Java, the .NET Framework includes a special class to be used when manipulating string objects: StringBuilder. The StringBuilder class includes methods for altering its contents. It is contained in the System.Text namespace. The following VB.NET sample shows it being used.
Imports System.TextModule MainModule
Sub Main()
Dim temp As StringBuilder
temp.Append("TechRepublic")
temp.Append(".net")
temp.Replace(".net", ".com")
temp.Append(" rules!")
Console.WriteLine(temp.ToString())
End Sub
End Module
And here's the C# equivalent:
using System.Text;namespace Builder {
class SBClass1 {
static void Main(string[] args) {
StringBuilder temp = new StringBuilder();
temp.Append("TechRepublic");
temp.Append(".net");
temp.Replace(".net", ".com");
temp.Append(" rules!");
Console.WriteLine(temp.ToString());
} } }
There are a few tradeoffs. There is overhead associated with creating a StringBuilder object, both in time and memory. On a machine with fast memory, a StringBuilder becomes worthwhile if you're doing about five operations. As a rule of thumb, I would say 10 or more string operations are a justification for the overhead on any machine, even a slower one.