Mintoris Forum

Author Topic: REDIM  (Read 2872 times)

jclemens

  • Sr. Member
  • ****
  • Posts: 146
REDIM
« on: Apr 04, 2013, 12:42 PM »
People have requested a REDIM statement in Mintoris. You can change the dimension of an existing array at any time with a new DIM statement.  What you CAN'T do is retain the contents of the array.  You can copy the contents of an array to a new one, give it a new dimension, and copy the contents back again, without horrendous performance overhead. Sadly you cannot copy the contents back with array copy because that truncates the destination array. So you have to copy nodes one at a time.  Below is an example of how to do this.

First we time setting the contents of an array of length 100000. Then we time doing it by starting with an array of 100 and periodically "rediming". In this example whenever we need more space we extend the array by 50%. The first loop for me took 3 seconds; the second one 14 seconds.

dim a(99999)
t0 = Time()
for i = 0 to 99999
    a(i) = 1
next i
print "elapsed: ",(Time()-t0)/1000
'
size = 100
dim b(size)
t0 = Time()
for i = 0 to 99999
if i >= size then
      size = ceil(size +size/2)
      ReDim(&b(),size)
endif
b(i) = i
next i
print "elapsed: ",(Time() - t0)/1000
end
'
Sub ReDim(arr(),length)
    x() = GetDim(arr())
    y() = arr()
    dim arr(length)
    for i = 0 to x(1)
        arr(i) = y(i)
    next i
End Sub
« Last Edit: Apr 04, 2013, 03:12 PM by jclemens »

sraffens

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 220
Re: REDIM
« Reply #1 on: Jun 29, 2014, 03:55 AM »
the Sub ReDim(&b(),size) took 34+ seconds
the 6.2 internal ReDir b(size) took 10+ seconds


jclemens

  • Sr. Member
  • ****
  • Posts: 146
Re: REDIM
« Reply #2 on: Jun 29, 2014, 06:54 AM »
That's to be expected.  Mark this post unnecessary.