Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String Addition (Optimization)
#11
(04-29-2024, 08:30 PM)DSMan195276 Wrote:
(04-26-2024, 02:19 AM)TerryRitchie Wrote: Holy cow. Can anything be done to improve QB64's string manipulation?
There are many improvements that could be made, the existing system has some pretty significant bugs as well as not being the most performant it could be. It requires someone to take the time to fix it though, that tends to be the limiting factor for a lot of things related to QB64 Big Grin

That said, there are some trade-offs that mean concatenation will always be slow. Many languages have this problem and introduce a kind of 'string builder' API to efficiently create strings, `MID$()` in QB64 like Steve used it is probably the closest thing to that.
Definitely hats off to the people that update and maintain the QB64 source code. When I first discovered QB64 I was immediately drawn to the source code hoping I could contribute in some way or the other. Then I looked at the code. I couldn't believe my eyes. I had no idea spaghetti and soup could be combined in such a manner. Sad 

I have not looked at the source code in ages. I'm sure it has improved over the years.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#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




Users browsing this thread: 1 Guest(s)