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)

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as