Repository navigation
Strings
π 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)π 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π 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 iWarning
When a pattern is built with Join(words, "|"), a ., (, +, $ or \
inside a word changes the pattern: escape each word first.
| 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".
| 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 |
π Figures: Excel 365 (16.0 build 20723) 64-bit, Windows desktop, median of 5 runs Β· Raw data in docs/benchmarks/
- π Home
- π Measurement Method
Topics
- π Working with Excel
- π¦ Passing Data
- π€ Strings
- π’ Numbers and Dates
- π Searching, Sorting and Collections
- π§© Calls, Classes and Binding
- π Files and CSV
- πͺ Windows API on 64-bit Office
Reference
β οΈ Correctness Traps- π§° Skill Rules