Benchmarks: strings

Ce document est disponible en français


String assignment, concatenation and comparison, and strings given to a procedure. How many times each loop runs is in its source.

The source is the module Strings.bas of the corpus built into rdc. Each badge gives RD-VBA's time as a multiple of MS-VBA's (how the figures are measured).

Cases

BenchStringAssignment

A String assigned in a loop.

Strings.BenchStringAssignment

Public Sub BenchStringAssignment()
    Dim I As Long
    Dim S As String
    For I = 1 To 100000
        S = "RD-VBA"
    Next
End Sub

BenchStringConcatenation

S = S & "x", a string growing by one character at a time.

Strings.BenchStringConcatenation

' each concatenation copies the string so far: the cost grows with its length, as it does in MS-VBA.
Public Sub BenchStringConcatenation()
    Dim I As Long
    Dim S As String
    For I = 1 To 10000
        S = S & "x"
    Next
End Sub

BenchStringComparison

Short strings joined and compared.

Strings.BenchStringComparison

' short Strings joined and compared: what parsing and formatting code does.
Public Sub BenchStringComparison()
    Dim I As Long
    Dim N As Long
    Dim S As String
    For I = 1 To 100000
        S = "RD" & "-VBA"
        If S = "RD-VBA" Then N = N + 1
    Next
End Sub

BenchStringBuilding

A line of numbers written as text, with a separator between each.

Strings.BenchStringBuilding

' a line built of numbers written as text, a separator between each: what a report or an export does, a conversion and two concatenations a value.
Public Sub BenchStringBuilding()
    Dim I As Long
    Dim S As String
    For I = 1 To 5000
        S = S & CStr(I) & ";"
    Next
End Sub

BenchStringArguments

Strings passed to a procedure, by value and by reference.

Strings.BenchStringArguments

' Strings given to a procedure, by value and by reference: what a helper that checks or edits text is given, a call a pass.
Public Sub BenchStringArguments()
    Dim I As Long
    Dim M As Long
    Dim N As Long
    Dim S As String
    Dim T As String
    S = "RD-VBA"
    For I = 1 To 100000
        M = Matched(S, "RD-VBA")
        N = N + M
        T = "RD"
        Suffixed T
    Next
End Sub

Private Function Matched(ByVal S As String, ByVal Expected As String) As Long
    If S = Expected Then Matched = 1
End Function

Private Sub Suffixed(ByRef T As String)
    T = T & "-VBA"
End Sub

See also


ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca