Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about _MEM blocks and arrays
#6
(06-04-2025, 04:21 PM)SMcNeill Wrote: A practical use for mem?   

Anything that requires speed.  Big Grin

Direct memory manipulation is much faster than referenced access to the same memory.   Array(index) has to first look up the index, then calculate the offset position of that index, then check the memory location for the data you want, then return it to you.  With mem, you specify the offset yourself, the amount of data you want, and presto, retrieve it.   

It's also much faster to shift an array in one chuck of movement than it is to shift it in a loop one element at a time, or to copy the whole array at once, and countless other things.

Consider this -- I have an array of 101 elements (DIM array(100) as _BYTE).   Now, I want to add an element to this array, but it needs to go in position number 52 and everything else shift down a spot.   

We could:

REDIM _PRESERVE array(101) AS _BYTE 'make it an extra byte in size bigger
FOR i = 100 to 52 STEP -1
   array(i+1) = array(i) 'shift everything right
NEXT
array(52) = new_data

OR we can:

REDIM _PRESERVE array(101) AS _BYTE
DIM m as _MEM: m = _MEM(array())
_MEMCOPY m, m.offset +52, 48 TO m, m.offset + 53  'shift those elements one byte over
_MEMFREE m
array(52) = new_data

One movement of memory to shift everything an element over, rather than doing it repeatedly in a loop.   In a loop of only going from 0 to 100, it's not going to make much difference in speed, but what if we're dealing with an array that holds 100,000,000 records and you need to shift 99,999,987 of them to make room for a new one?   Moving it all at once makes a heckuva difference then, compared to shifting it with a loop.   Then imagine that this shifting is something you might be doing 500,000 times as you're inserting new records into your existing database and are using a binary insertion sort on them...

The whole point of mem is speed.  If that's not something you have to be concerned about, then just use arrays and indexes as they're much easier to track and intuitive to work with.  But when it comes time that your program is reaching a bottleneck in performance, you may have resort to using mem to keep things running fast enough to be usable without wasting all your time while it's working in the background on some trivial task with countless looping.
   Ok,  I was being thick....    That's a very good point.    Honestly I don't think of speed often on modern system's (very spoiled that way).     Usually things are *Fast Enough* even with inefficient code.     But I'll keep that in mind for future use.    Actually my X16 Video convertor is at the point where it could use some optimization.    Not sure How much speed I can pick up because the whole approach is pretty inefficient as I'm actually shelling ImageMagick for Each and Every Frame of the Video  and a HUGE amount of the program's execution is spent in that loop. But my palette shuffling routines also make a difference in the overall execution, perhaps I can shed some execution time there.
Reply


Messages In This Thread
RE: Question about _MEM blocks and arrays - by ahenry3068 - 06-04-2025, 04:31 PM



Users browsing this thread: 2 Guest(s)