Cluj München 1140km

Gânduri clujene din Bavaria

München - Munich - Monaco di Baviera

1. iulie 2009 13:27
by skorpionking
0 Comentarii

Visual Basic 6.0 string operations performance - faster string concatenation

1. iulie 2009 13:27 by skorpionking | 0 Comentarii

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 String
Private lOffset As Long

Public Sub ReInit()
    sBuffer = ""
    lOffset = 0
End 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)
            sBuffer = sBuffer & String$(BUFFER_CHUNK_SIZE, 0)
        End If
    End If

    Mid(sBuffer, lOffset + 1, lNewStrLen) = NewString
    lOffset = lOffset + lNewStrLen
End Sub 

Some other tips:

  • don't compare with empty string like this If str = "" Then but If  Len(str) = 0 is much faster
  • Replace is not fast so if you are not sure that the string will be replaced each time, but only 50% of the times, check first with InStr
  • InStrRev is very slow, also StrConv and CStr are very slow.
  • Binary comparison vbBinaryCompare is faster then vbTextCompare
  • when you need to compare characters, use Asc(W)/Chr(W) like this If AscW(Mid$(Text$, index)) = 65 Then
  • avoid Like and StrConv if possible

Here are some other usefull links:

Happy programming :-) and watch out, the old dinosaur VB 6 ain't yet dead :-)

Adaugă comentariu