Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help to speed up searching multidim arrays...

Status
Not open for further replies.

LamerThanU

Technical User
Feb 1, 2007
8
US
Well i guess ill start with what code i have and work from there...

zNum = 0
x2 = 0
y2 = 0
z2 = 0

Do
Do
Do
myArray(x2, y2, z2) = zNum
If zNum = txtNum.Text Then
GoTo subEnd
End If
z2 = z2 + 1
zNum = zNum + 1
Loop Until z2 = 99
z2 = 0
y2 = y2 + 1
Loop Until y2 = 99
y2 = 0
z2 = 0
x2 = x2 + 1
Loop Until x2 = 99

subEnd:
txtX2.Text = x2
txtY2.Text = y2
txtZ2.Text = z2

Ok, if you havent already figured it out, im filling about 970 thousand elements with whatever math alg but for now im just using x++. if i get rid of the if...end if, all 970k elements are filled in a split second but i dont know how to test or search for a specific element number with out the if...end if which you probly know slows it down alot. it takes about 45 secs to test up to 350k elements.

my question is... can i fill the array (which takes less than a second) and then find a particular number based on a number i type in a text box with some kind of search, then pass the array numbers x2,y2,z2 to 3 text boxes like before? my goal is to be able to search all 970k elements in 10 secs or less.

thank you in advance for any help... hopefully there is a solution it is just beyond me.
 
From what you are showing there seems to be no reason to use a multidim array. 3 linear arrays will store the data. If the data is always monotonic increasing then a simple binary search algo will get there much faster.

___________________________________________________________
If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
Steam Engine Prints
 
And, to elaborate on analysis of OP's code, all her datatypes are variants, which will slow everything down. Considering that the if requires implicit type-conversion, this is a really awful bit of code.

Dim the array cells as the proper numeric type, and OUTSIDE the loops, convert your txtnum.text to the same numeric type. You can do this because you never change it.
 
>it takes about 45 secs to test up to 350k elements.

Are you saying that running your code as it stands (with, say, -1 in the txtBox so we never get a match a populate al 970000 odd records) takes 45 seconds? I ask, because the code only takes 5 seconds to run on my PC ...
 
But hard to say what is happening in the editbox events for txtX2, txtY2, txtZ2
 
Well, the OP has already stated that simply removing the if ... end if from the code reduces the runtime down to 'a split second' and, given the txtX2, txtY2 and txtZ2 assigments are done whether the if ... end if is there or not, I'd suggest they are not contributing any significant delay to the problem as it is expressed here.
 
John, I don't think you can replace the 3D array with 3 linear arrays. This is a 3-"dimentional" array presumably containing 99[sup]3[/sup] = 970299 elements. Three linear arrays of 99 elements will only store 99 x 3 = 297 elements.

>because the code only takes 5 seconds to run on my PC ...

Mine took roughly 8 seconds. And after incorporating harebrain's recommendations it reduced to 0.1 seconds.
 
>Mine took roughly 8 sec

So I don't think we're seeing the actual code that LamerThanU is actually running, and therefore it is difficult to give appropriate advice (although harebrain's recommendations are definitely worth incorporating).
 
>given the txtX2, txtY2 and txtZ2 assigments are done >whether the if ... end if is there or not

>>GoTo subEnd

Right, I looked too quickly and instead seeing of a Goto, somehow saw a blurred Gosub (which wouldn't make sense).
 
Hypetia, I agree if the question is going to be generalised. The array OP is currently showing has multiply redundant info, hence my question.

The fact that OP is suggesting filling the array with a function led me to the question of monotonicity, and thence to binary search.

___________________________________________________________
If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
Steam Engine Prints
 
The logic of the loop could (aside of using other than variant types) speed up the loop:
1. txtNum can be converted to Long (say, lngNum),
2. lngNum can be used to calculate Num1, Num2 and Num3 (Num1+Num2*99+Num3*99*99=lngNum), that would be equivalent to digits representing lngNum in 99-imal number system. Your txtX2, txtY2,txtX2 are the same as those numbers.
3. build three loops using For..Next (first with three nested loops, second with two, and the third single loop, starting from 0, till given Num or 99.


combo
 
strongm said:
So I don't think we're seeing the actual code that LamerThanU is actually running
LamerThanU said:
im filling about 970 thousand elements with whatever math alg but for now im just using x++.
Consider that the "x++" construct appears in the inner loop and is the operative expression:
Code:
        Do
            myArray(x2, y2, z2) = zNum
            If zNum = txtNum.Text Then
                GoTo subEnd
            End If
            z2 = z2 + 1
            zNum = zNum + 1
        Loop Until z2 = 99
Replacing "zNum = zNum + 1" with a more complex arithmetic expression, as stated by OP, we're back to the use of variants as being the culprit; the more complex the expression, the bigger the performance hit. Credit strongm and Hypetia for the proof.
 
To continue, that would take care of filling the array.

To accomplish the search, I'd consider flatting the whole structure into a one-dimensional array of records with two fields: the result of the algorithm, and the [x, y, z] that produced the result. Sort the array on the result field and use a binary search.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top