Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: StringBuilder for ThinBASIC @ GitHub

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736

    StringBuilder for ThinBASIC @ GitHub

    Hi all,

    I am sure you are all familiar with the concept of StringBuilder - it is little helper which performs efficient string concatenations and often much more. I started working on such a component for ThinBASIC - the language has incredibly epic string handling, but didn't have any stringbuilder... till now!

    The usage
    ... is very simple. You instantiate object, and then use its methods:
    Uses "StringBuilder", "Console"      
    
    Function TBMain()
    
      Dim stringBuffer As String
      Dim sb As StringBuilder
      sb = New StringBuilder()
      
      Long i
            
      Dim stopWatch As cTimer
      stopWatch = New cTimer()
      
      Print "Appending to classic string: "
      stopWatch.Start       
      For i = 1 To 20000
        stringBuffer += "Appending is fine!"
      Next             
      stopWatch.Stop
      PrintL stopWatch.ElapsedToString(%CTIMER_MILLISECONDS, "0") + " ms (Total " + Len(stringBuffer) + " characters)"
      
      Print "Appending to string builder: "
      stopWatch.Start       
      For i = 1 To 20000
        sb.Add("Appending is fine!")
      Next             
      stopWatch.Stop
      PrintL stopWatch.ElapsedToString(%CTIMER_MILLISECONDS, "0") + " ms (Total " + Len(sb.ToString()) + " characters)"
      
      PrintL
      PrintL "Press any key to quit..."
      
      WaitKey
      
    End Function
    
    What is it good for?
    It is great for efficient string appending. Do you see the code above? Check the speed on your PC or see the screenshot...

    The source code
    I decided to make the code public, to make possible for others to commit their enhancements. Get the source @GitHub:
    https://github.com/petrSchreiber/thi..._StringBuilder

    The binaries
    Please note StringBuilder is official part of thinBasic since 1.9.16.17! Yay!
    You can still grab the latest release from GitHub as well.

    Documentation
    The documentation is provided at Wiki associated with the repository, please check here:
    https://github.com/petrSchreiber/thi...ngBuilder/wiki

    Petr
    Attached Images Attached Images
    Last edited by Petr Schreiber; 17-12-2016 at 11:59.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  2. #2
    Member mike lobanovsky's Avatar
    Join Date
    Aug 2009
    Location
    Omnipresent
    Posts
    191
    Rep Power
    41
    Hi Petr,

    The timings are really impressive.

    How would you comment on the following script and the associated snapshot, please?

    Uses "StringBuilder", "Console"     
     
    Function TBMain()
     
      Long i
             
      For i = 1 To 2000000
        SpawnStringBuilder
      Next            
    
      PrintL "Press any key to quit..."
       
      WaitKey
       
    End Function
    
    Sub SpawnStringBuilder()
      Dim sb As StringBuilder
      sb = New StringBuilder()
    
      sb.Capacity = 10000
      sb.Clear()
    End Sub
    
    Attached Images Attached Images
    Mike
    (3.6GHz i5 Core Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, x64 Windows 7 Ultimate Sp1)

  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    Hi Mike,

    well, that cries "memory leak" I am looking into it right now...


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    Thank you Mike,

    I forgot the destruction must be handled on module side - now fixed.

    Please redownload the DLL, and if curious, check the change in commit at git.

    Early versions of the SDK freed the object, but when we discussed with Eros possiblity of non-COM based objects, it was changed to be freed by module creator.

    From thinBasic programmer point of view, all objects are garbage collected - destuctor is called once they run out of scope.


    Petr
    Last edited by Petr Schreiber; 02-08-2014 at 21:30.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5
    Member mike lobanovsky's Avatar
    Join Date
    Aug 2009
    Location
    Omnipresent
    Posts
    191
    Rep Power
    41
    Thanks a lot Petr,

    In fact, git was the first place I poked my nose into. Now everything seems to be running just fine even without the sb.Clear() call.
    Mike
    (3.6GHz i5 Core Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, x64 Windows 7 Ultimate Sp1)

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    Updated the module:
    • .Add method can now take any number of parameters


    Few tips on when to use this:
    Uses "StringBuilder", "Console"      
    
    Function TBMain()
    
      Dim stringBuffer As String
      Dim sb As StringBuilder
      sb = New StringBuilder()
      
      Long i
            
      Dim stopWatch As cTimer
      stopWatch = New cTimer()
      
      PrintL "Appending to classic string: "
      stopWatch.Start       
      For i = 1 To 20000
        stringBuffer += "Appending is fine! (" + i + ")"
      Next             
      stopWatch.Stop
      PrintL stopWatch.ElapsedToString(%CTIMER_MILLISECONDS, "0") + " ms (Total " + Len(stringBuffer) + " characters)"
    
      PrintL
      PrintL "Appending to classic string with StrFormat$: "
      stringBuffer = ""
      stopWatch.Start       
      For i = 1 To 20000
        stringBuffer += StrFormat$("Appending is fine! ({1})", i)
      Next             
      stopWatch.Stop
      PrintL stopWatch.ElapsedToString(%CTIMER_MILLISECONDS, "0") + " ms (Total " + Len(stringBuffer) + " characters)"
    
      PrintL
      PrintL "Appending to string builder with StrFormat$: "
      stopWatch.Start       
      For i = 1 To 20000
        sb.Add(StrFormat$("Appending is fine! ({1})", i))
      Next             
      stopWatch.Stop
      PrintL stopWatch.ElapsedToString(%CTIMER_MILLISECONDS, "0") + " ms (Total " + Len(sb.ToString()) + " characters)"
        
      PrintL
      PrintL "Appending to string builder with multiple parameters: "
      sb.Clear()
      stopWatch.Start       
      For i = 1 To 20000
        sb.Add("Appending is fine! (", i, ")")
      Next             
      stopWatch.Stop
      PrintL stopWatch.ElapsedToString(%CTIMER_MILLISECONDS, "0") + " ms (Total " + Len(sb.ToString()) + " characters)"
      
      PrintL
      printl "Press any key to quit..."
      
      WaitKey
      
    End Function
    

    Petr
    Attached Images Attached Images
    Last edited by Petr Schreiber; 14-07-2016 at 20:12.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  7. #7
    Hi Petr
    in the wiki you said :Place the thinBasic_stringBuilder.dll to Mod directory inside your thinBasic installation
    but it seems the binary thinBasic_stringBuilder.dll is from 2015 while the source code is from 2016. even the old dll works but i want to be sure it is the correct file
    the dll i found inside thinBasic_StringBuilder.zip.
    thanks

  8. #8
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    Hi Primo,

    thanks for letting me know. StringBuilder is now part of ThinBASIC default installation, you don't have to get it from GitHub, if you don't want to.
    The binaries there are fresh!:
    https://github.com/petrSchreiber/thi.../tag/1.9.16.17


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  9. #9
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    I prepared new version of stringBuilder for thinBasic 1.9.16.18.
    Users of older version can download the binaries here: https://github.com/petrSchreiber/thi.../tag/1.9.16.18

    What is new? I added the DataPtr read-only property, which is handy for cases when you want to work with stringBuilders internal memory directly, for enhanced performance.
    Full documentation here: https://github.com/petrSchreiber/thi...ngBuilder/wiki


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  10. #10
    Thanks Petr for the additional property DataPtr
    i have tried it like this, but not sure if this is the correct usage:
    Uses "StringBuilder", "Console" 
    Dim i As Long
    
    Dim sb As New StringBuilder
      
    sb.Add("Ciao world")
    sb.Add("Ciao world")
      
    Dim s As String * 4 At sb.DataPtr 
     
    PrintL s 
    Long pointer
    pointer = sb.DataPtr 
    String st 
    For i=1 To 20
    st = Peek$(pointer, i)
    PrintL st
    Next
    
    
    WaitKey
    

Page 1 of 2 12 LastLast

Members who have read this thread: 0

There are no members to list at the moment.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •