Results 1 to 10 of 13

Thread: StringBuilder for ThinBASIC @ GitHub

Hybrid View

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,798
    Blog Entries
    3
    Rep Power
    693

    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 Thumbnails Attached Thumbnails Click image for larger version. 

Name:	StringBuilderIsFast.png 
Views:	119 
Size:	31.8 KB 
ID:	9070  
    Last edited by Petr Schreiber; 17-12-2016 at 11:59.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  2. #2
    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 Thumbnails Attached Thumbnails Click image for larger version. 

Name:	StrBldr_Mem.PNG 
Views:	102 
Size:	41.3 KB 
ID:	9072  
    Mike
    (3.2GHz i5 Core Quad w/ 16GB RAM, 2 x GTX 650Ti SLI-bridged w/ 2GB VRAM, x64 Windows 7 Ultimate Sp1)

  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,798
    Blog Entries
    3
    Rep Power
    693
    Hi Mike,

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


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,798
    Blog Entries
    3
    Rep Power
    693
    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 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  5. #5
    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.2GHz i5 Core Quad w/ 16GB RAM, 2 x GTX 650Ti SLI-bridged w/ 2GB VRAM, x64 Windows 7 Ultimate Sp1)

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,798
    Blog Entries
    3
    Rep Power
    693
    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 Thumbnails Attached Thumbnails Click image for larger version. 

Name:	StringBuilderMultiAdd.png 
Views:	93 
Size:	7.6 KB 
ID:	9214  
    Last edited by Petr Schreiber; 14-07-2016 at 20:12.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

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
  •