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



Users browsing this thread: 6 Guest(s)