05-06-2025, 07:43 AM
Hi
thanks to Steve I can post this here because I have a STRING$ function that fills fastly a string with a pattern but I have missed the thread about String concatenation and MID$ way String concatenation thread.
By the way in a speed test this function is faster than MID$ way!
So I share it here.
Wellcome feedbacks and improvements of FillString or STRING$pattern (what name is more explicative?),
maybe any other friend of QB64pe wants share something better for performance and or algorythm.
In the other thread there is the screenshot with comparisons and a graphic explanation of why it works well.
thanks to Steve I can post this here because I have a STRING$ function that fills fastly a string with a pattern but I have missed the thread about String concatenation and MID$ way String concatenation thread.
By the way in a speed test this function is faster than MID$ way!
So I share it here.
Code: (Select All)
Dim As Long Scr
Scr = _NewImage(800, 600, 32)
Screen Scr
_ControlChr Off
_Title "FillString: a new STRING$"
Cls , _RGB32(33, 172, 172)
Locate 2, 1: Print "Starting Test :"
S$ = Chr$(0) + Chr$(255) + Chr$(255) + Chr$(255) ' Substring
Fs$ = "" ' FinalString
Size = 800 * 600 * 4 '1.920.000
Print "Fillstring concatenation with 2*n formula"
t1# = Timer(0.001)
Print FillString(Size, S$, Fs$), Size, Len(Fs$)
t2# = Timer(0.001)
Print " Fillstring with MID$"
Fs$ = "" ' FinalString
t3# = Timer(0.001)
Print FillString2(Size, S$, Fs$), Size, Len(Fs$)
t4# = Timer(0.001)
Print "Fillstring concatenation with n+n formula"
Fs$ = "" ' FinalString
t5# = Timer(0.001)
Print SlowFillString(Size, S$, Fs$), Size, Len(Fs$)
t6# = Timer(0.001)
Locate 10, 1
Print Using " Tempo & #.#### #.#### #.#### "; "String = String + String "; (t2# - t1#); t1#; t2#
Print Using " Tempo & #.#### #.#### #.#### "; "MID$ way "; t4# - t3#; t3#; t4#
Print Using " Tempo & #.#### #.#### #.#### "; "String = String + Pattern "; t6# - t5#; t5#; t6#
End
Function FillString (Size As Long, Bases As String, S As String)
FillString = 0
S = Bases
Do
S = S + S
Loop Until Len(S) > Size
S = Left$(S, Size)
FillString = -1
End Function
Function FillString2 (Size As Long, Bases As String, S As String)
FillString2 = 0
Dim posi As Long
S = Space$(Size)
posi = 0
Do
Mid$(S, posi, 4) = Bases
posi = posi + 4
Loop Until posi > Size
S = Left$(S, Size)
FillString2 = -1
End Function
Function SlowFillString (Size As Long, Bases As String, S As String)
SlowFillString = 0
Dim As Double Starts, Ends
Dim Counter As Long
Starts = Timer(.001)
Ends = 10#
Counter = 0
Do
Counter = Counter + 1
S = S + Bases
Loop Until Len(S) > Size Or (Timer(.001) - Starts >= Ends)
Print , Counter; " cycles", Len(S); " lenght of string vs max size"; Size
S = Left$(S, Size)
SlowFillString = -1
End Function
Wellcome feedbacks and improvements of FillString or STRING$pattern (what name is more explicative?),
maybe any other friend of QB64pe wants share something better for performance and or algorythm.
In the other thread there is the screenshot with comparisons and a graphic explanation of why it works well.

