Skip to content

Strings

Daniele Penza edited this page Dec 1, 2026 · 1 revision

πŸ”€ Strings

Evidence Top cost

πŸ”΄ Building a string with S = S & X

πŸ“ Each append copies everything built so far: 11x a buffer at 10,000 pieces, 35x at 30,000. The checker reports it as PERF003.

Build a string of N numbers S = S & X Mid$ buffer Join
N = 10,000 8.2 ms 0.7 ms 1.3 ms
N = 30,000 77.6 ms 2.2 ms 4.0 ms
' βœ… simplest: collect the pieces, join once
Dim parts() As String, i As Long, s As String
ReDim parts(1 To n)
For i = 1 To n
    parts(i) = CStr(i)
Next i
s = Join(parts, vbNullString)            ' use vbCrLf to build file lines
' βœ… fastest: write into a buffer that doubles when full
Dim buf As String, cap As Long, pos As Long, piece As String
cap = 1024: buf = Space$(cap): pos = 1
For i = 1 To n
    piece = CStr(i)
    Do While pos + Len(piece) - 1 > cap
        buf = buf & Space$(cap)              ' runs about log2(n) times
        cap = cap * 2
    Loop
    Mid$(buf, pos, Len(piece)) = piece
    pos = pos + Len(piece)
Next i
s = Left$(buf, pos - 1)

🟠 Scanning character by character with Mid$

πŸ“ About 62 ns per character. To find a character, InStr is 16x faster; to process every character, a Byte array is 5x faster.

Count spaces in 1,000,000 characters Median Ratio
InStr loop 4 ms 1.0x
Byte array, step 2 12 ms 3.1x
Len(s) - Len(Replace(s, " ", "")) 27 ms 6.7x
Mid$ loop 62 ms 15.8x
' βœ… jump from match to match
p = InStr(1, s, " ", vbBinaryCompare)
Do While p > 0
    n = n + 1
    p = InStr(p + 1, s, " ", vbBinaryCompare)
Loop

' βœ… process every character without temporary strings
Dim b() As Byte, j As Long
b = s                                       ' UTF-16: two bytes per character
For j = 0 To UBound(b) Step 2
    If b(j) = 32 And b(j + 1) = 0 Then n = n + 1
Next j

🟠 Like and InStr before RegExp

πŸ“ VBScript.RegExp costs 3.6 us to create and 0.8 us per Test.

Task Fastest RegExp
ID is one letter and three digits t Like "[A-Z]###": 60 ns 13x if created once, 61x if created per call
Count 12 month names in 100 KB 12 InStr loops: 0.5 ms one alternation Execute: 24x slower
' βœ… create once, outside the loop, and only for patterns Like cannot express
Dim re As Object
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "^\d{4}-\d{2}-\d{2}$"
For i = 1 To n
    If re.Test(Values(i)) Then Hits = Hits + 1
Next i

Warning

When a pattern is built with Join(words, "|"), a ., (, +, $ or \ inside a word changes the pattern: escape each word first.

⚠️ Turning text into numbers

Expression Result with an Italian locale (decimal ,)
CDbl("1.5") πŸ“ 15, silently: the point is the thousands separator
CDbl("1,5") 1.5
Val("1,5") πŸ“ 1: Val ignores the locale and stops at a comma
IsNumeric("1e5"), ("(5)"), ("5-"), ("&H10"), (" 42 "), ("€5") πŸ“ all True

βœ… Check the exact format first (for example digits only, with Like or an AscW loop), then convert. IsNumeric answers "can VBA coerce this", not "is this a plain number".

🟒 Small or negligible

Advice Result
Left$, Trim$, UCase$ instead of Left, Trim, UCase πŸ“ 1.16x. ⚠️ Left$(Null, 1) raises error 94; Left(Null, 1) returns Null
UCase$(t) = LCase$(t) to detect letters πŸ“ 1.9x slower than Like "*[A-Za-z]*", but only it sees accented letters
Swap long strings while sorting πŸ“ 190 ns per swap; swapping two Long indices is 12x faster: sort an index array
Fixed-length String * 30 arrays πŸ“ 1.3-2.7x, on a total of 15 ms for 100,000 strings

⚑ VBA Performance

Topics

Reference

Clone this wiki locally