|
Speed Up Your VB.NET Code
Optimization rules have changed under VB.NET. Here are eight new ways to build wicked-fast code.
by Francesco Balena
August 2002 Issue
Technology Toolbox: VB.NET
VB.NET changes the way you write Visual Basic code. You learn quickly that most of the optimization tricks you've learned for VB6 won't work under VB.NET. For example, .NET memory allocation forces you to rethink how you process strings and other data types. In other cases, the problem isn't in the language. For instance, ADO.NET requires optimization techniques completely different from those for ADO.
I'll show you the eight more useful tips for making your VB.NET programs run like the wind. You can apply most of these techniques to C# as well because .NET languages are actually just a thin layer over the .NET Framework. Note that I took all timings by compiling the code without debug support and after ticking the Enable Optimizations option in the Configuration Properties | Optimizations page of the Project Properties dialog box.
1 Concatenate Strings With StringBuilder
Your first optimization trick involves sidestepping string concatenation. Consider this slow code:
' Create a comma-delimited list of all
' numbers in the range [1-10000]
Dim i As Integer, s As String
For i = 1 To 10000
s &= CStr(i) & ", "
Next
Back to top
|