Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String Addition (Optimization)
#12
Hi
thanks to Steve I read this thread and I can give my 2 cents

I was TRYING to make a STRING$ that fill a string variable of fixed size with a pattern and not with the first character/ASCII code of a pattern. The actual STRING$ of Qbasic is SPACE$ extended to all ASCII codes.
I arrive at this interesting point that share here.

A different algorythm of concatenation of strings beats the MID$ way of creating a large string. 
We all know that the standard concatenation of strings has been buried by the MID$ way.

here the comparison code with the 3 functions Fillstring, Fillistring2 (MID$), SlowFillString (older way of concatenation strings).

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

here a screenshot of the Steve's original code showing how fast is MID$ way vs  S$ = S$+ Added$, 
and of the demo posted above with the 3 way to fill a large string with a substring.
On the right of the image there is the explanation of why  algorythm S = S+k is slower than S = S+S with a graphline green for S+k and a graphline red for  S+S (that is 2*n) 

[Image: Fillstring-a-new-STRING-beats-MID-way.jpg]
Reply


Messages In This Thread
String Addition (Optimization) - by SMcNeill - 04-26-2024, 12:48 AM
RE: String Addition (Optimization) - by Jack - 04-26-2024, 01:28 AM
RE: String Addition (Optimization) - by Jack - 04-26-2024, 01:54 AM
RE: String Addition (Optimization) - by SMcNeill - 04-26-2024, 03:21 AM
RE: String Addition (Optimization) - by Dimster - 04-26-2024, 01:22 PM
RE: String Addition (Optimization) - by RhoSigma - 04-26-2024, 02:22 PM
RE: String Addition (Optimization) - by mdijkens - 04-26-2024, 02:48 PM
RE: String Addition (Optimization) - by bplus - 04-26-2024, 06:29 PM
RE: String Addition (Optimization) - by TempodiBasic - 05-06-2025, 07:31 AM



Users browsing this thread: 1 Guest(s)