Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
STRING$ empowered with StringPatternFilling
#1
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.

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.
Reply


Messages In This Thread
STRING$ empowered with StringPatternFilling - by TempodiBasic - 05-06-2025, 07:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Split String to Array Using Strtok (Attempt #2) SpriggsySpriggs 0 531 12-17-2024, 06:37 PM
Last Post: SpriggsySpriggs
  PrintW - print a long string, breaking it at the last space or hyphen before col. 79 TDarcos 21 4,018 04-22-2024, 09:52 PM
Last Post: Pete
  Fonts from String Patterns bplus 15 3,676 08-30-2023, 03:01 PM
Last Post: grymmjack
  String to Array AtomicSlaughter 1 738 02-13-2023, 10:11 PM
Last Post: mnrvovrfc
  Remove Spaces (or other characters) from a String George McGinn 10 2,909 12-31-2022, 12:21 PM
Last Post: euklides

Forum Jump:


Users browsing this thread: 1 Guest(s)