QB64 Phoenix Edition

Full Version: replace good old join$ with Bind2$
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
inspired by steves addstrings, i modified join$ to speed it up.
join$(array$(), delimiter$) was for uniting an array into a single string with delimiters like commas, colons or nothing between the items in array.

Code:
_Title "Bind$ test" ' b+ 2024-04-28
'  now testing bind2$(arr$(), delimiter$)
'  Function bind$ (arr$()) is my mod of steves code
Const limit = 20000
Dim Shared NumStr(1 To limit) As String

MakeNumsStrings
t# = Timer(0.001)
o$ = Join$(NumStr(), ":") 'time how long it takes to add those strings together fro mjoin$
t1# = Timer(0.001)
o1$ = bind2$(NumStr(), ",") 'and time how long it takes to just mid$ those strings, if you know the size
t2# = Timer(0.001)
o2$ = MidStrings$(Len(o$))
t3# = Timer(0.001)
Print "Results:"
Print "First 50: "; Left$(o$, 50)
Print "First 50: "; Left$(o1$, 50)
Print "First 50: "; Left$(o2$, 50)
Print "Last  50: "; Right$(o$, 50)
Print "Last  50: "; Right$(o1$, 50)
Print "Last  50: "; Right$(o2$, 50)
Print
Print
Print Using "It took ###.### seconds to      join$"; t1# - t#
Print Using "It took ###.### seconds to  testbind$"; t2# - t1#
Print Using "It took ###.### seconds to midstrings"; t3# - t2#


Sub MakeNumsStrings
    For i = 1 To limit
        NumStr(i) = _Trim$(Str$(i))
    Next
End Sub

Function AddStrings$
    For i = 1 To limit
        temp$ = temp$ + NumStr(i)
    Next
    AddStrings = temp$
End Function

Function MidStrings$ (size)
    temp$ = Space$(size)
    p = 1 'position in full string
    For i = 1 To limit
        Mid$(temp$, p) = NumStr(i)
        p = p + Len(NumStr(i))
    Next
    MidStrings = temp$
End Function

Function bind$ (arr$())
    Dim As Long lb, ub, i, size, p
    Dim rtn$
    lb = LBound(arr$)
    ub = UBound(arr$)
    For i = lb To ub
        size = size + Len(arr$(i))
    Next
    rtn$ = Space$(size)
    p = 1
    For i = lb To ub
        Mid$(rtn$, p) = arr$(i)
        p = p + Len(arr$(i))
    Next
    bind$ = rtn$
End Function

Function bind2$ (arr$(), bindchar$) ' string concat is so slow, this should work faster than join
    Dim As Long lb, ub, lbc, i, size, p
    Dim rtn$
    lb = LBound(arr$)
    ub = UBound(arr$)
    lbc = Len(bindchar$)
    For i = lb To ub - 1
        size = size + Len(arr$(i)) + lbc
    Next
    size = size + Len(arr$(ub))
    rtn$ = Space$(size)
    p = 1
    For i = lb To ub - 1
        Mid$(rtn$, p) = arr$(i)
        p = p + Len(arr$(i))
        Mid$(rtn$, p) = bindchar$
        p = p + lbc
    Next
    Mid$(rtn$, p) = arr$(ub)
    bind2$ = rtn$
End Function

Function Join$ (arr() As String, delimiter$)
    Dim i As Long, b$
    For i = LBound(arr) To UBound(arr)
        If i = LBound(arr) Then b$ = arr(LBound(arr)) Else b$ = b$ + delimiter$ + arr(i)
    Next
    Join$ = b$
End Function

[attachment=3178]

oh it looks like size for midstrings does not have to be exact, I just noticed i fed it a bigger string length than it needed.