Re: Question about 2D VB arrays
- From: "Dmitriy Antonov" <antonovdima@xxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 7 Nov 2006 23:32:54 -0500
"John" <John@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:62313F40-A0FC-4E8A-A14D-2E925F38873A@xxxxxxxxxxxxxxxx
Dmitriy,
You state the following:
"So if you want to have the same memory layout in both languages, then
you
just
need to switch "meaning" of subscripts to the opposite - if you think
that
the first
dimension in C is a row, then you should think that the first subscript in
VB is a column."
When you stated this, it leads one to believe that one of the langues will
now be
more inefficient when accessing the array. If the array is stored in
contiguous
memory in column major order (i.e. VB) and accessed by C, the C code will
be much more inefficient, due to the fact that it expects it's data to be
in
row
major order. There is going to be a big hit due to memory access.
John
What do you mean? Do you mean that there is a difference in access time to
different elements of the same array? Shouldn't. But if you think so then
put VB code into "disadvantageous" position (from you point of view) - VB
code will not really suffer from such "treatment", especially comparing to
efficiency of C code.
Additionally, as it was mentioned here, you can use the same technique in VB
to avoid any confusion. This is direct (unlike your example) equivalent of
your C code in VB:
dim i&, j&, k&
dim larray(480& * 640 - 1) as long
for i=0 to 480 -1 ' loop for rows
for j=0 to 640 - 1' loop for columns
larray(i * 640 + j) = k' row * columns + column
next j
next i
erase larray
Unless I overlooked something, it will give you exactly the same memory
layout and the same access algorithm. To squeeze every CPU cycle I would
replace (640-1) with its result for the price of clarity (I am not sure that
VB compiler will optimize it).
And if we go back to 2D array in VB, you still, as I said, can achieve the
same result (same memory layout) if you change the meaning of dimensions. If
you thought that the first dimension is a row and the second is a column,
then just think opposite. For example in the following snippet I just
slightly changed your sample:
Dim i As Long, j As Long, k As Long
Dim larray(0 To 479, 0 To 639) As Long
k = 0
For i = 0 To 639 ' columns
For j = 0 To 479 ' rows
larray(j, i) = k
k = k + 1
Next j
Next i
It is a little bit late and there can be a lot of mess in my head, but it
seams to me that result will be the one that you want. Again, there should
be no any reputable documentation, which dictates you which dimension
represents rows and which one - columns - it's all about convention.
Dmitriy.
.
- References:
- Re: Question about 2D VB arrays
- From: John
- Re: Question about 2D VB arrays
- Prev by Date: Re: Regedit
- Next by Date: What is "Quote" ?
- Previous by thread: Re: Question about 2D VB arrays
- Next by thread: how to find big number from listbox
- Index(es):
Relevant Pages
|