» Home » VB Fibre
Site
News
Files

Visual Basic
Strings
Math
General
Properties
Memory
Methods

Search
Testing

Inline ASM-VB
Strings
Math
General
Memory

Search
Using inline ASM
Submit!

Arrays: Temporary variables

Well no doubt about it. Temporary variables are going to speed up your program. I have to say that I had to wait longer than expect to complete this routine. Anyway, if you're often pointing to an array, you've definetly got to use temporary values. Check the source code for an example. Note that I've done the test 5 times, but there was only 1 main iteration, cause the For...Next loops where long enough. Also note that in the first test without the temporary variable, I didn't declare the "lngTemp" variable. We didn't use it there, so no extra variable declaration.

Used code:

Code:
Public Sub TestOne() Dim I As Long, J As Long Dim myArray(1 To 50000) As Long For I = 1 To 50000 For J = I + 1 To 50000 If myArray(I) = myArray(J) Then End If Next Next Erase myArray End Sub Public Sub TestTwo() Dim I As Long, J As Long Dim myArray(1 To 50000) As Long Dim lngTmp As Long For I = 1 To 50000 lngTmp = myArray(I) For J = I + 1 To 50000 If lngTmp = myArray(J) Then End If Next Next Erase myArray End Sub

No temp var % faster than Temp var No temp var (sec) Temp var (sec)
49,8% 23,928901 15,979177
49,8% 23,919381 15,963122
48,5% 23,946439 16,122013
48,4% 23,893372 16,103921
48,9% 23,896261 16,050374


User contributed notes:

Author: Comintern () Date: 22:03 15/03/2005
This is almost certainly caused by cache misses. The temp variables would fit in the cache, whereas the whole array would not. You might want to check with a smaller array and see if you still get the same results.

Author: Tom (hurendo_kun at hotmail dot com) Date: 16:06 08/06/2005
Then I guess the lesson here is to use temp vars with larger arrays. ;)

Add user-note
Author:
E-mail (optional):
Anti spam, please enter 'ok' without quotes:
Comment: