Special Reports

Optimize VB.NET Code Performance

Optimization rules have changed under VB.NET -- here's half a dozen new ways to build wicked-fast code.

Optimizing a VB.NET application often requires a deep understanding of how the .NET Framework works, including details about the inner workings of the garbage collector and the Just In Time (JIT) compiler. In some cases, you even must redesign your entire app to leverage .NET features such as class inheritance or ADO.NET batch updates. However, a few simple tricks can often speed up your code without affecting the app's structure.

If you're sure your integer operations never throw an exception, select the "Remove integer overflow checks" option in the Configuration Properties | Optimizations page of the Project Properties dialog box. This option can speed up integer operations by 30 to 40 percent.

The compiler can produce better code in some cases if you use the new Return statement to return a value from a function, instead of assigning the value to the variable named after the function itself (as you did in VB6).

Use the new Try?Catch statement instead of the old-style On Error statements, which were preserved only to ease the migration from VB6. In fact, the VB compiler translates On Error statements into hidden Try?Catch blocks.

If you use old-style On Error statements, at least try not to use On Error Resume Next, Resume, or Resume Next statements. These cause the compiler to add hidden statements to keep track of the line about to be executed, so that control flow can resume from the correct point in your code.

When comparing strings in a case-insensitive way, try using String.CompareOrdinal instead of the String.Compare method or the = and <> operators. In fact, the former method compares the numeric codes of individual characters and doesn't take locale into account, helping make it six or seven times faster than the latter methods.

You can often speed up searches and replace string operations dramatically if you use regular expressions. For example, this code displays all the individual words in a sentence:

' This code requires the following imports
'  Imports System.Text.RegularExpressions

Dim s As String = "Hi there, how are you?"
Dim re As New Regex("\w+")
Dim m As Match
For Each m In re.Matches(s)
    Console.WriteLine(m.Value)
Next

You can learn more on this topic by reading about the Regex class and the System.Text.RegularExpressions namespace in the .NET SDK documentation.

About the Author

Francesco Balena has authored several programming books, including Programming Microsoft Visual Basic .NET Version 2003 [Microsoft Press]. He speaks regularly at VSLive! and other conferences, founded the .Net2TheMax family of sites, and is the principal of Code Architects Srl, an Italian software company that offers training and programming tools for .NET developers. Reach him at [email protected].

comments powered by Disqus

Featured

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

Subscribe on YouTube

Upcoming Training Events