Hi all, who thought that VB 6.0 is dead? Recently I had to optimize a VB 6.0 LOB application and a problem was that strings operations are very slow in Visual Basic 6.0 .Unfortunately, many of the string functions are not optimized for speed. This is why VB6 apps may run slower than necessary. Fortunately, you can overcome many of the speed limitations by clever coding. The followin tips are based on Visual Basic 6.0 and variable-length strings. They're most useful with string-intensive programs that read, parse or manipulate large amounts of text. The performance gains from using these techniques are significant if you're executing the calls thousands or hundreds of thousands of times. If you're just occasionally writing and reading a few strings outside of loops, these tips won't help you much.

Perhaps the biggest bottleneck is that VB makes copies of the string data when doing some of the operations. Even when you're just reading strings (and not planning to make any modifications), you can easily end up making a large number of copies. The copying costs you time if string processing is an intensive part of your program. Another reason is that some of the widely used functions are implemented in a non-straightforward way. They may be doing more work than what is required for your task. Fortunately, you can often replace an advanced functions with a simpler and faster alternative.

For example, concatenating strings with & may be slow if you do it very often. The following class, let' say cFastStringConcatenation, speeds up the things very good:

Option Explicit On

Private Const BUFFER_CHUNK_SIZE As Integer = 15000 ' or you can play with this value and set it according to your needs

Private sBuffer As StringPrivate lOffset As Long

Public Sub ReInit() sBuffer = "" lOffset = 0End Sub

' use this to initialize the string with a value Public Sub SetString(ByRef Source As String) sBuffer = Source & String(BUFFER_CHUNK_SIZE, 0) lOffset = Len(source)End Sub

' this function returns the value of the string in the buffer Public Function GetString() As String GetString = Left(sBuffer, lOffset)End Function

' this function is used to concatenate a new string into the buffer Public Sub Concat(ByRef NewString As String) Dim lNewStrLen As Long

lNewStrLen = Len(NewString)

'Allocate more space in buffer if needed If (lOffset + lNewStrLen) >= Len(sBuffer) Then If lNewStrLen > lOffset Then sBuffer = sBuffer & String$(lNewStrLen, 0) Else sBuffer = sBuffer & String$(BUFFER_CHUNK_SIZE, 0) End If End If