Dirk Goldgar said:
I'm off to do some benchmarks ...
.... And here they are. Module code was this (wrapped by newsreader):
'----- start of code -----
Option Compare Database
Option Explicit
Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type
Private Declare Function QueryPerformanceCounter Lib "kernel32"
(lpPerformanceCount As LARGE_INTEGER) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32"
(lpFrequency As LARGE_INTEGER) As Long
Private Function LargeToDec(arg As LARGE_INTEGER) As Variant
LargeToDec = arg.lowpart + 2 * CDec(arg.highpart) * 2 ^ 31
End Function
Sub subDirksVersion(ByRef strInput As String)
Dim strPrint As String
Dim lngStart As Long
For lngStart = 1 To Len(strInput) Step 30
strPrint = Mid$(strInput, lngStart, 30)
Next lngStart
End Sub
Sub subVansVersion(ByRef strInput As String)
Dim strPrint As String
Do While Len(strInput) > 0
strPrint = Left(strInput, 30)
strInput = Mid(strInput, 31)
Loop
End Sub
Sub BenchStringSplitter(Optional NTIMES As Long = 1)
Dim result As Variant
Dim freq As LARGE_INTEGER
Dim starting As LARGE_INTEGER
Dim ending As LARGE_INTEGER
Dim dfreq As Variant
Dim lngCount As Long
Dim strOriginal As String
Dim strWork As String
QueryPerformanceFrequency freq
dfreq = LargeToDec(freq)
strOriginal = String(32767, "A")
' Make sure both functions have been loaded.
strWork = strOriginal
subDirksVersion strWork
strWork = strOriginal
subVansVersion strWork
' Test with long string.
strOriginal = String(32767, "A")
QueryPerformanceCounter starting
For lngCount = 1 To NTIMES
strWork = strOriginal
subVansVersion strWork
Next lngCount
QueryPerformanceCounter ending
Debug.Print "Van's version, string length = " & _
Len(strOriginal) & ": ", _
(LargeToDec(ending) - LargeToDec(starting)) / dfreq
QueryPerformanceCounter starting
For lngCount = 1 To NTIMES
strWork = strOriginal
subDirksVersion strWork
Next lngCount
QueryPerformanceCounter ending
Debug.Print "Dirk's version, string length = " & _
Len(strOriginal) & ": ", _
(LargeToDec(ending) - LargeToDec(starting)) / dfreq
' Test with short string.
strOriginal = String(90, "A")
QueryPerformanceCounter starting
For lngCount = 1 To NTIMES
strWork = strOriginal
subVansVersion strWork
Next lngCount
QueryPerformanceCounter ending
Debug.Print "Van's version, string length = " & _
Len(strOriginal) & ": ", _
(LargeToDec(ending) - LargeToDec(starting)) / dfreq
QueryPerformanceCounter starting
For lngCount = 1 To NTIMES
strWork = strOriginal
subDirksVersion strWork
Next lngCount
QueryPerformanceCounter ending
Debug.Print "Dirk's version, string length = " & _
Len(strOriginal) & ": ", _
(LargeToDec(ending) - LargeToDec(starting)) / dfreq
' Test with zero-length string.
strOriginal = vbNullString
QueryPerformanceCounter starting
For lngCount = 1 To NTIMES
strWork = strOriginal
subVansVersion strWork
Next lngCount
QueryPerformanceCounter ending
Debug.Print "Van's version, string length = " & _
Len(strOriginal) & ": ", _
(LargeToDec(ending) - LargeToDec(starting)) / dfreq
QueryPerformanceCounter starting
For lngCount = 1 To NTIMES
strWork = strOriginal
subDirksVersion strWork
Next lngCount
QueryPerformanceCounter ending
Debug.Print "Dirk's version, string length = " & _
Len(strOriginal) & ": ", _
(LargeToDec(ending) - LargeToDec(starting)) / dfreq
End Sub
'----- end of code -----
And the results of executing with loop count of 100:
----- copied from the Immediate Window -----
benchstringsplitter 100
Van's version, string length = 32767:
1.9502730095584774042510989525
Dirk's version, string length = 32767:
0.0465799424228498314729944728
Van's version, string length = 90:
0.0003774222701488597014425018
Dirk's version, string length = 90:
0.0001877333571724897996812444
Van's version, string length = 0: 0.000049726990441522595748901
Dirk's version, string length = 0: 0.00005280000670476275616035
----- end copy -----
Obviously it makes a difference how many times you go through the loop.
Also, I had to leave a string assignment in the loop for both functions,
because subVansVersion is destructive of the original argument string.