Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Undocumented trick to speed up functions that return array
[ All Languages » VB »  Arrays]

Total Hit ( 2668)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


VB6 functions can return an array. Unlike regular functions that return scalar values or objects, however, you can't use the name of the function as a local variable where to store intermediate result, and you are forced to work with a temporary local array, and then assign this array to the Function name before exiting, as in:

Click here to copy the following block
' Returns an array of N random elements
' and stores their average in AVG
Function GetRandomArray(ByVal n As Long, avg As Single) As Single()
  Dim i As Long, sum As Single
  ReDim res(1 To n) As Single
  ' fill the array with random values and keep a running total  
  Randomize Timer
  For i = 1 To n
    res(i) = Rnd
    sum = sum + res(i)
  Next
  ' assign the result array then evaluate the average
  GetRandomArray = res
  avg = sum / n
End Function

Unbelievably, the above routine can be made faster by simply inverting the order of the last two statements, as in:

Click here to copy the following block
' ...
  ' evaluate the average THEN assign the result array
  avg = sum / n
  GetRandomArray = res
End Function

For example, on a Pentium II 333MHz machine, when N is 100,000 the former routine runs in 0.72 seconds, while the latter runs in 0.66 seconds, and is therefore 10% faster.
The reason for this odd behavior is that if the former case VB copies the res array into the GetRandomArray result value, and when the array is large this takes a sensible amount of time. In the latter case, the assignment statement is the very last in the procedure, therefore the VB compiler is sure that the temporary res array can't be used again, and instead of copying it the compiler simply swaps its array descriptor with the descriptor of the GetRandomArray value, thus saving both the physical copy of array elements and the subsequent deallocation of the memory used by the res array.

Summarizing, when writing a Function that returns an array, ensure that the statement that assigns the local array to the return value is immediately followed by a Exit Function or End Function statement (remarks are ignored in this case).





Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.