Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
REDIM takes more resources than DIM?
#35
Some background context regarding arrays:

QuickBasic has two separate kinds of arrays, static and dynamic. Static arrays have their bounds known at compile time, and so the compiler can emit more efficient code. Dynamic arrays are the opposite, their bounds are not known until the program reaches the statements at runtime.

When you write DIM a(x), the compiler must create a dynamic array as the value of x is not known until the program runs. When you write DIM a(3), the compiler can be more efficient and create a static array.

Here is QB 7.1 creating and accessing a dynamic array (apologies for screenshots over text, it is awkward to get listings out of codeview):


[Image: Screenshot-20240723-224753.png]
First we must call the B$DDIM helper routine with arguments specifying lower (0) and upper (the value of x) bounds, element size (4) and number of dimensions (1). When we access an element, we first read the array lower bound from memory, add that to the index we want then finally access the array.

Whereas here is creating and accessing a static array:

[Image: Screenshot-20240723-225844.png]

Since the compiler knows the size of the array, space is already reserved for it and no initialisation is required. This is why you can't redim a static array: it has its fixed spot in memory. Because the lower bound is also known at compile time, the memory address of each element can be emitted directly.

So static arrays are more efficient, but their efficiency relies on them being at a fixed location in memory with a fixed size. Whereas dynamic arrays can be resized, but move around in memory and need extra memory accesses to calculate offsets.

QB64 does not make the same degree of optimisation regarding static arrays, but still enforces the restriction on resizing them. I suppose in the future it could optimise this, but there are probably smarter ways to do it.

PS This is one time CONST makes a difference. CONST x = 3: DIM a(x) gives a static array, without the CONST it would be a dynamic array.
Reply


Messages In This Thread
REDIM takes more resources than DIM? - by bplus - 07-17-2024, 12:21 AM
RE: REDIM takes more resources than DIM? - by luke - 07-23-2024, 01:04 PM



Users browsing this thread: 3 Guest(s)