1.

Solve : VB6 speed question?

Answer»

If I have a STRING array in VB and I need to access a certain element of it many times, is it faster to store it in a separate string variable first?

Here is an ex of what I mean:
Code: [Select]PRIVATE Function GetSuffix(delim As String) As String
Dim i As Integer

i = InStr(sl(sln), delim)
GetSuffix = Right(sl(sln), Len(sl(sln)) - i)
End Function

OR

Code: [Select]Private Function GetSuffix(delim As String) As String
Dim i As Integer
Dim theStr As String

theStr = sl(sln)

i = InStr(theStr, delim)
GetSuffix = Right(theStr, Len(theStr) - i)
End Function

I understand that in the code I posted above the speed would probably not really be effected because sl(sln) is accessed only a few times. But what if I had it in a for loop or something. Would it be better the first or second way?It would be faster to store it in a temp variable, otherwise your application will perform a safearray lookup each time.

The speed difference depends on the size of the loop; I just MADE a small test and the array lookup version was about a tenth of a microsecond slower then the one that used a temporary variable.

In the case of the function EXAMPLE... it might make sense to use a parameter, rather then a otherwise mysterious element of a global or module-level array.


Quote

It would be faster to store it in a temp variable
Ok. I'll do it that way.

Quote
mysterious element of a global or module-level array
Yes, it is a global array. sl is an array which holds the code and sln is the current line number being read by the interpreter.

Quote
it might make sense to use a parameter
I had it that way originally, but it got annoying to have to always type that extra parameter.


Discussion

No Comment Found