Faster string appending with Mid$ command

Faster string appending with Mid$ command

As you probably know, the “&” operator is rather slow, especially with long strings. When you have to repeatedly append chucks of characters to the same variable, you can speed up your code using a simple trick based on the Mid$ command. The idea is that you pre-allocate a buffer longenough to accomodate for the result of your operation. Here’s an example of this technique.

Suppose you want to create a string given by appending the first 10000integers, e.g. “1 2 3 4 5 6 7 … 9999 10000”. This is the easiest way to do so:

res = ""For i = 1 to 10000: res = res & Str(i): Next

The problem with the standard approach is that the res variable is reallocated 10000 times. This is a better way to reach the same result:

Dim res As StringDim i As LongDim index As Long' prepare a buffer long enoughres = Space(90000)' this points to where we want to insert the stringindex = 1' here we goFor i = 1 to 10000    substr = Str(i)    length = Len(substr)    ' stuff the string inside the result variable    Mid$(res, index, length) = substr    ' advance index    index = index + lengthNext' drop extra charsres = Left$(res, index - 1)

The standard code takes 2.2 seconds on my 333 MHz system computer, while the smart way takes only 0.08 seconds, so it’s 25+ times faster.

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