Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing fixed length string array to function
#9
SUB MemSmush (m AS _MEM, x%) '                                  move array elements above x% down over x%. Fill uppermost with null
    _MEMCOPY m, m.OFFSET + (m.ELEMENTSIZE * (x% + 1)), m.SIZE - (m.ELEMENTSIZE * (x% + 1)) TO m, m.OFFSET + (m.ELEMENTSIZE * x%)
    _MEMFILL m, m.OFFSET + m.ELEMENTSIZE * ((m.SIZE / m.ELEMENTSIZE) - 1), m.ELEMENTSIZE, 0 AS _BYTE
END SUB


Seems to me like this would be a mem routine where you could speed up the performance on it considerably, with just a little reworking of the math.  (This is a nice example of what I was talking about with the Optimizing Code section of my QB64 Bible.)


SUB MemSmush (m AS _MEM, x%) '                                  move array elements above x% down over x%. Fill uppermost with null
    DIM o AS _OFFSET
    o = (m.ELEMENTSIZE * x%)
    'Would not m.ELEMENTSIZE * (x% +1) be equivelant to (o + m.ELEMENTSIZE)?? <-- removes the multiplication from the math
    'And I just predistributed the minus to the -(o + m.ELEMENTSIZE) for after the second comma 

    _MEMCOPY m, m.OFFSET + o + m.ELEMENTSIZE), m.SIZE - o - m.ELEMENTSIZE TO m, m.OFFSET + o <-- 3 multiplication calculations less than previously used
    _MEMFILL m, m.OFFSET + m.ELEMENTSIZE * ((m.SIZE / m.ELEMENTSIZE) - 1), m.ELEMENTSIZE, 0 AS _BYTE
END SUB


Also seems to me that "m.ELEMENTSIZE * ((m.SIZE / m.ELEMENTSIZE) - 1)" could be simplified down some.

x * ((y / x) - 1)  (to simplify typing)
x* (y/x) - x  'distribute the x
y - x 'the x's cancel
y - x

Or, back to the original:   m.SIZE - m.ELEMENTSIZE

SUB MemSmush (m AS _MEM, x%) '                                  move array elements above x% down over x%. Fill uppermost with null
    DIM o AS _OFFSET
    o = (m.ELEMENTSIZE * x%) 'replace those two simple equations with a single variable to do the math once.
    'Would not m.ELEMENTSIZE * (x% +1) be equivelant to (o + m.ELEMENTSIZE)?? <-- removes the multiplication from the math
    'And I just predistributed the minus to the -(o + m.ELEMENTSIZE) for after the second comma 

    _MEMCOPY m, m.OFFSET + o + m.ELEMENTSIZE), m.SIZE - o - m.ELEMENTSIZE TO m, m.OFFSET + o '<-- 3 multiplication calculations and 1 negation less than previously used
    _MEMFILL m, m.OFFSET + m.SIZE - m.ELEMENTSIZE, m.ELEMENTSIZE, 0 AS _BYTE '<-- Removed multiplication and division operation
END SUB


Now, I don't have your whole program to swap things in to test it out with, but unless our math gurus see something which I've did wrong, I'd think that that final routine should be the exact same as the first one, except without it having to do a lot of the math calculations with those mem routines.  The final MemSmush should be quite a bit faster and more efficient than the original MemSmush you posted.  Wink
Reply


Messages In This Thread
RE: Passing fixed length string array to function - by SMcNeill - 08-25-2022, 11:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Star Suggestion for new REPLACE$() function zaadstra 3 262 01-26-2026, 05:11 PM
Last Post: grymmjack
Question Experimenting with a "StringList" type for simpler handling of string arrays/lists Heimdall 18 1,227 12-19-2025, 12:51 PM
Last Post: Heimdall
  Is there a menu function? Mad Axeman 17 1,095 12-17-2025, 04:43 AM
Last Post: SMcNeill
  Sub not Reconizing Dim as String pmackay 18 1,476 10-16-2025, 03:32 PM
Last Post: pmackay
  getting the number of dimensions in an array on the fly? madscijr 7 745 09-16-2025, 12:34 AM
Last Post: madscijr

Forum Jump:


Users browsing this thread: 1 Guest(s)