Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BadLimit
#9
George posted a routine written in C to remove characters from a string, which I was going to use to compare performance between it and my own routine inside the BadLimit stuff above.  Much as I expected, the pure C routine was quite a bit faster than what I had, so I thought I'd try and redo my routine to make it a little faster.  The result of that attempt is below:

Code: (Select All)
check$ = "This is a string that we're going to check to see how long it would take to remove the spaces from."
For i = 1 To 12
    check$ = check$ + check$
Next
Print "The length of our test string is:"; Len(check$)
t# = Timer
new$ = RemoveCharacters$(check$, " ", 0)
t1# = Timer

Print Len(new$)
Print Using "###.#### seconds"; t1# - t#
Do: _Limit 10: Loop Until _KeyDown(27)
Print new$

Function RemoveCharacters$ (passed_from_what As String, what_character As String, from_which_side As Integer)
    'from_which_side: 1 = left, 2 = right, 3 = left and right, 0 = whole string
    Dim from_what As String: from_what = passed_from_what
    If Len(from_what) = 0 Then Exit Function
    Dim m As _MEM, p As _Offset, l As _Offset, a As _Unsigned _Byte
    a = Asc(what_character)
    If from_which_side = 0 Then
        $Checking:Off
        m = _MemNew(Len(from_what))
        _MemPut m, m.OFFSET, from_what
        l = m.SIZE
        Do Until o >= l
            If _MemGet(m, m.OFFSET + o, _Unsigned _Byte) = a Then
                _MemCopy m, m.OFFSET + o + 1, l - o - 1 To m, m.OFFSET + o
                l = l - 1
            Else
                o = o + 1
            End If
        Loop
        from_what = Space$(l)
        _MemGet m, m.OFFSET, from_what
        $Checking:On
    Else
        If from_which_side And 1 Then
            i = 1
            Do Until Asc(from_what, i) <> a
                i = i + 1
            Loop
            from_what = Mid$(from_what, i)
        End If
        If from_which_side And 2 Then
            i = Len(from_what)
            Do Until Asc(from_what, i) <> a Or i = 0
                i = i - 1
            Loop
            from_what = Left$(from_what, i)
        End If
    End If
    RemoveCharacters = from_what
End Function


My old function took about 10 times longer to run and do its thing, than the newly mem-powered version here.  If there's a way for us to eeek out any more performance, just using QB64 commands alone, I'm not seeing it at the moment.  George's routine with headers and all might be faster (I'll wait for him to get a few little glitches out before running any speed comparison tests between the routines), but this is quick enough for my needs.  It's not all that often that I need to strip massive amounts of characters from a string, after all.  Wink
Reply


Messages In This Thread
BadLimit - by SMcNeill - 12-16-2022, 03:50 AM
RE: BadLimit - by mnrvovrfc - 12-16-2022, 11:01 AM
RE: BadLimit - by SMcNeill - 12-16-2022, 12:03 PM
RE: BadLimit - by SpriggsySpriggs - 12-21-2022, 02:53 PM
RE: BadLimit - by SMcNeill - 12-21-2022, 03:20 PM
RE: BadLimit - by mnrvovrfc - 12-21-2022, 03:53 PM
RE: BadLimit - by SpriggsySpriggs - 12-21-2022, 04:06 PM
RE: BadLimit - by SpriggsySpriggs - 12-21-2022, 04:25 PM
RE: BadLimit - by SMcNeill - 12-29-2022, 10:40 AM
RE: BadLimit - by MrCreemy - 12-29-2022, 06:31 PM



Users browsing this thread: 1 Guest(s)