Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A = A(0)
#4
DIM A AS INTEGER
DIM A(0) AS INTEGER

Let's take an in depth look at both of the above, why don't we?

First, DIM A AS INTEGER -- this is a declaration that we're adding a single variable to our program, and that it's going to be an INTEGER type.

Then, DIM A(0) AS INTEGER -- this is a declaration that we're going to add an array called A, with an allowable range from 0 to 0, in our program, and it's going to be an INTEGER type.

Now, as far as useful needs inside a program goes, there's honestly not going to be that much difference.  Any place where you could use A, you could use A(0), and vice-versa.  After all, at the end of the day, both are nothing more than single reference points to an integer value in memory.

BUT, there's a big difference in the overhead that comes from those two statements:

For A, as an integer, looking up and using it's value is as simple as just finding the offset, getting the value, and then working with that portion of memory.   If we say "A is an integer at byte 1234 in memory", that's all the machine needs to know to work with the value of A inside our program.  

Now, for A(0), being an array, things get a little more complicated.  First, we have to tell the pc where that array is located in memory.  Then it has to know the type of element that's in that array.  Then it's got to move forward element_size * index, to find the proper portion of memory to work with.  It's a case of saying, "A is an array at byte 1234 in memory, and it's all integers of 2-bytes each, and we want the 0th element of it, so the data we need is at position 1234 + 2 * 0, so we want to look at byte 1234 and get 2 bytes of information."

As you can hopefully see from the wordiness of that statement, there's a lot more to look up and deal with before we interact with A(0), than there is in just dealing with A.

Arrays inherently carry more overhead to them, than just a simple variable does.  They use more processing power, execute slower, and even use more memory overall (as they tend to have larger descriptors associated with them to account for their element range and size and whatnot).  As bplus pointed out, there's even more typing associated with using A(0) over just A.

Honestly, I can't think of any good reason why someone would make use of A(0), instead of just A.  Unless that array is one which you're going to REDIM later and build upon, I honestly don't see how A(0) would ever be more efficient or easier than just using A (or even a simple A0) in your code.
Reply


Messages In This Thread
A = A(0) - by Dimster - 01-18-2024, 04:56 PM
RE: A = A(0) - by SpriggsySpriggs - 01-18-2024, 05:01 PM
RE: A = A(0) - by bplus - 01-18-2024, 06:27 PM
RE: A = A(0) - by SMcNeill - 01-18-2024, 10:02 PM
RE: A = A(0) - by Dimster - 01-19-2024, 03:25 PM
RE: A = A(0) - by SMcNeill - 01-19-2024, 04:52 PM
RE: A = A(0) - by Dimster - 01-19-2024, 05:55 PM
RE: A = A(0) - by NakedApe - 01-19-2024, 07:12 PM



Users browsing this thread: 2 Guest(s)