Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: VB6 speed question  (Read 2902 times)

0 Members and 1 Guest are viewing this topic.

Linux711

    Topic Starter


    Mentor

    Thanked: 59
    • Yes
    • Programming Blog
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
VB6 speed question
« on: February 07, 2011, 06:20:40 PM »
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?
YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: VB6 speed question
« Reply #1 on: February 07, 2011, 06:36:53 PM »
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.


I was trying to dereference Null Pointers before it was cool.

Linux711

    Topic Starter


    Mentor

    Thanked: 59
    • Yes
    • Programming Blog
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: VB6 speed question
« Reply #2 on: February 07, 2011, 06:48:01 PM »
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.
YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe