devxlogo

Faster String Processing

Faster String Processing

String processing in VB is usually a very slow task. Often, people process strings letter by letter and construct a larger trinf afterwards from small bits. A better way would be to look at the strings the way VB handles them: as arrays.

Consider these 2 code snippets:

Public Function EncodeToBase64String(DecryptedText As String) As String  Dim c1, c2, c3 As Integer  Dim w1 As Integer  Dim w2 As Integer  Dim w3 As Integer  Dim w4 As Integer  Dim n As Long  Dim retry As String   For n = 1 To Len(DecryptedText) Step 3    c1 = Asc(Mid$(DecryptedText, n, 1))    c2 = Asc(Mid$(DecryptedText, n + 1, 1) + Chr$(0))    c3 = Asc(Mid$(DecryptedText, n + 2, 1) + Chr$(0))    w1 = Int(c1 / 4)    w2 = (c1 And 3) * 16 + Int(c2 / 16)    If Len(DecryptedText) >= n + 1 Then w3 = (c2 And 15) * 4 + Int(c3 / 64)Else w3 = -1    If Len(DecryptedText) >= n + 2 Then w4 = c3 And 63 Else w4 = -1    retry = retry + mimeencode(w1) + mimeencode(w2) + mimeencode(w3) +mimeencode(w4) Next EncodeToBase64String = retryEnd Function

There are improvements you could make to this code to speed it up:

  1. Generate a string with the correct length when you start. Since every time you need to create a bigger string, VB needs to generate a new one.
  2. You no longer need MID$ and ASC since you can access the bytes already.
  3. Avoud using len(Decryptedtext) to determine the counter.

Here is what the imrpoved code looks like:

Public Function EncodeToBase64StringNew(DecryptedText As String) As String  Dim c1 As Integer, c2 As Integer, c3 As Integer  Dim w1 As Integer  Dim w2 As Integer  Dim w3 As Integer  Dim w4 As Integer  Dim n As Long  Dim m As Long  Dim retry As String  Dim nLength As Long  Dim arString() As Byte  Dim arResult() As Byte  ReDim arResult(CLng(Len(DecryptedText)  3) * 8 + 8)  arString() = DecryptedText  nLength = Len(DecryptedText)   For n = 1 To nLength Step 3    c1 = arString((2 * n) - 2)    c2 = arString(2 * n)    If 2 * (n + 1) < UBound(arString) Then        c3 = arString(2 * (n + 1))    Else        c3 = 0    End If    arResult(m) = Asc(mimeencode(c1  4))    m = m + 2    arResult(m) = Asc(mimeencode((c1 And 3) * 16 + c2  16))    m = m + 2    If nLength >= n + 1 Then        arResult(m) = Asc(mimeencode((c2 And 15) * 4 + c3  64))        m = m + 2    Else        arResult(m) = 0        m = m + 2    End If    If nLength >= n + 2 Then        arResult(m) = Asc(mimeencode(c3 And 63))        m = m + 2    Else        arResult(m) = 0        m = m + 2    End If Next EncodeToBase64StringNew = arResultEnd Function

All these changes result in a speed increase of a factor 200 for the final function (using a 92 KB Textfile as input)

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist