|
-
Dec 13th, 2007, 05:31 PM
#1
Thread Starter
Lively Member
Faster way to loop through a two-dimensional array?
Hi.
When I loop through a two dimensional array I use:
Code:
Dim i, i2, MyArray(3, 20) As Integer
For i = 0 To UBound(MyArray, 1)
For i2 = 0 To UBound(MyArray, 2)
Next i2
Next i
Is there a faster/better way to do so?
Also another question not related to the topic title.
I have been looking through a few threads and have seen code like the following
Function Something(int1 as integer, _
What does the _ do?
-
Dec 13th, 2007, 05:42 PM
#2
Re: Faster way to loop through a two-dimensional array?
Second question first; easier. The space underscore is a code line break. The code continues but to another line. Makes reading long lines of code a bit easier to some vs scrolling right left to read it.
First question. Looping is looping. Unless you have the array sorted somehow, there really isn't a faster way to find something in it. If it is sorted, then there is faster way. If you are looping for other reasons then searching, then there may be faster ways, depending on what you are trying to do. More specifics would be helpful.
-
Dec 13th, 2007, 05:51 PM
#3
Thread Starter
Lively Member
Re: Faster way to loop through a two-dimensional array?
Second question first; easier. The space underscore is a code line break. The code continues but to another line. Makes reading long lines of code a bit easier to some vs scrolling right left to read it.
Thanks makes sense.
First question. Looping is looping. Unless you have the array sorted somehow, there really isn't a faster way to find something in it. If it is sorted, then there is faster way. If you are looping for other reasons then searching, then there may be faster ways, depending on what you are trying to do. More specifics would be helpful.
Say I wanted to set all the contents of an array to a value.
Would the the code below be the best choice of doing so?
Code:
Dim i, i2, MyArray(1, 1) As Integer
For i = 0 To UBound(MyArray, 1)
For i2 = 0 To UBound(MyArray, 2)
MyArray(i, i2) = 5
Next i2
Next i
-
Dec 13th, 2007, 06:40 PM
#4
Re: Faster way to loop through a two-dimensional array?
You want to set each variable individually, because unlike in .NET, VB6 doesn't declare all variables according to the last datatype set in the line. The current code is equal to:
Code:
Dim i As Variant, i2 As Variant, MyArray(1, 1) As Integer
If you want to set all items to zero, the fastest you can do is Erase MyArray. However, there are memory tricks that could be applied for faster setting to a certain value, in this case that you're using a two dimensiona array: make a fake 1-dimensional array of equal amount of items and then set the value. This however is so technical and bad on the speed/value/amount of code range that it's not worth to do it in your every day code.
In the other hand, if you really need speed, you'd be using one dimensional array. For one reason or the other, accessing one dimensional arrays is faster in VB6 than their multidimensional counterparts. And it can have a great effect in speed if you're doing something that would otherwise be lightweight math, but requires a lot of looping. Using \ and Mod operators to simulate X and Y for two dimensionality in one dimensional arrays can be a faster solution.
Optimizations are always dependant on what you're trying to achieve. Roughly there are two kinds of optimizations: methods that are faster for a computer to calculate than other possible alternatives, and better algorithms. Understanding what happens "behind the scenes" also has a great deal of effect on code quality you end up producing.
Most important thing is that if something works fast enough, you don't need to optimize it. There are other things however that have their effect, such as formatting code properly, naming conventions of variables, declaring all variables, using the correct datatypes, keeping code nicely organized etc. - which are more important than pure speed.
-
Dec 13th, 2007, 07:49 PM
#5
Re: Faster way to loop through a two-dimensional array?
Another approach.
Code:
Option Explicit
Private Type MyType
Int0 As Integer
Int1 As Integer
Int2 As Integer
Int3 As Integer
End Type
Private MT(20) As MyType
Private Sub Form_Load()
Dim i As Long
For i = 0 To UBound(MT)
Debug.Print MT.Int0
Debug.Print MT.Int1
Debug.Print MT.Int2
Debug.Print MT.Int3
Next i
End Sub
-
Dec 14th, 2007, 09:49 PM
#6
Re: Faster way to loop through a two-dimensional array?
 Originally Posted by Disyne
Hi.
When I loop through a two dimensional array I use:
Code:
Dim i, i2, MyArray(3, 20) As Integer
For i = 0 To UBound(MyArray, 1)
For i2 = 0 To UBound(MyArray, 2)
Next i2
Next i
I'd cache the result of UBound(MyArray,2) in a variable to start. In the outer loop it won't make much difference (UBound would only be called once), but inside the inner loop it gives you a performance hit. Although in your case you're using a static array so it you should be using constants to begin with.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|