Suggestions to increase the performance of searching my array?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I wonder if anyone has suggestions for reducing the amount of time it would take to search my array using the function that I have written. I want to find a position in the array of an item that matches on all three variables. Suggestions?

Public Shared Function GetArrayPosition(ByVal ipRemoteEndPoint As String, ByVal intEngine_Type As Integer, ByVal intEngine_ID As Integer)
For i As Integer = 0 To aryDevice.GetUpperBound(1)
If aryDevice(0, i) = ipRemoteEndPoint.ToString And aryDevice(1, i) = intEngine_Type And aryDevice(2, i) = intEngine_ID Then
Return i
End If
Next
Return -1
End Function
 
How can you create an array include different datatype (String, Integer).
I suggest you use datatable.
 
Dan,

First, you don't want to mix types in an array. I noticed you have String and
Integer types.
If all 3 dimensions of your array reference a single item, then I might use an
array of a Structure with a fixed length string.

Second, do not perform multiple tests in a single line using the "And" operator.
VB must resolve all the expressions prior to evaluating your comparison test.
In cases where the first test fails, it will still perform the other two tests
along with all the unnecessary overhead.
VB.Net does include the "AndAlso" and "OrElse" operators which will provide a
"Short-Circuit" method of evaluation.
Meaning the first test to fail will cause the whole expression to fail, and
return False without evaluating the subsequent expressions.
IMHO, I prefer to break them into separate tests for readability.
In addition, it also allows you to perform other operations between tests, as in
the case of indexing.

Third, along with the second, perform your tests in the order which will most
likely give you a False first.
This will allow you to minimize the number of unnecessary comparisons.
Sometimes that fastest way to get to your desired item is to eliminate as many
of the items as quickly as possible that cannot fulfill your criteria.

Fourth, if you have a large number of elements in the array and you will be
accessing them a lot, then I recommend pre-sorting the array on an appropriate
key. You could also create Index arrays with pointers into the primary array and
use this to minimize the number of elements to test.
The best implementation of an Index, if even appropriate, would be dependant
upon the contents of the array and intended usage.

Hope this helps,
Gerald


Dan said:
I wonder if anyone has suggestions for reducing the amount of time it would
take to search my array using the function that I have written. I want to find
a position in the array of an item that matches on all three variables.
Suggestions?
Public Shared Function GetArrayPosition(ByVal ipRemoteEndPoint As String,
ByVal intEngine_Type As Integer, ByVal intEngine_ID As Integer)
For i As Integer = 0 To aryDevice.GetUpperBound(1)
If aryDevice(0, i) = ipRemoteEndPoint.ToString And aryDevice(1, i)
= intEngine_Type And aryDevice(2, i) = intEngine_ID Then
 
You can use timers and timespans and set up test cases.
Get your start time, then process a whole bunch of them, then get end time and
determine the elapsed time.
Do the same for other solutions.
Run them a bunch of times looking for consistency, since a whole mess of things
beyond your control could affect the outcome of a single test.
Make sure your test case will give you a good mix criteria, as one method might
be able to find a particular value much faster than another, but might be much
slower at finding a different value.

Rough sample would be:

Dim startTime, endTime As Date
Dim elapsed As TimeSpan
startTime = Now

'Do your work here

endTime = Now
elapsed = endTime.Subtract(startTime)


Gerald

Dan said:
Those are excellent ideas and I will give them a try. I find myself now
wondering how I can tell whether one solution is faster than the other?
Recognizing that we are talking about miliseconds here, but is there a built in
way in VB.Net to time the execution of a portion of code?
 
I thought that in VB.Net the if statements had changed such that if the first expression was false, then the following expressions after and were not evaluated..is this not the case?
 
Yes, this is not the case. :-)
IF, AND, OR are bitwise operators that also conveniently serve as logical
boolean operators.
In the early days, MS had planned on implementing, and they actually made it
into Beta, seperate operators for bitwise comparison; such as BitAnd, BitOr,
etc.
However, that broke old programs and confused the VB6 converts.
So they removed them and went back to the old method.

However, they did add the AndAlso and OrElse short-circuitable logical operators
to help to deal with the inefficiency of using the bitwise operators for logical
operations. Personally, I like them seperate and was looking forward to the
seperation, but history prevailed.

Gerald

Dennis said:
I thought that in VB.Net the if statements had changed such that if the first
expression was false, then the following expressions after and were not
evaluated..is this not the case?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top