Visual Basic Tip: 11/06/95


Optimization Tips.
* Avoid the use of the Variant variable type.  Especially when dealing
  with variables containing numeric data.  The default variable type is
  variant in VB3.  Explicitly declare your variables or use the appropriate
  variable suffix (% = integer, & = long, ! = single, ...).  Also consider
  the use of Option Explicit to ensure that all variables are declared.

  compare                 speed improvement
  ----------------------------------------- 
  integer vs.  variant          27%
  long    vs.  variant          23%
  single  vs.  variant          40%
  double  vs.  variant          36%
  string  vs.  variant           0%


* Use the variable type that uses the smallest amount of memory and meets requirements.

  type     bytes            range
  ------------------------------------------------------------------------------------- 
  integer    2                 -32,768 to        32,767
  long       4          -2,147,483,648 to 2,147,483,647

  single     4          -3.402823        E38   to -1.401298        E-45  for neg values
                         1.401298        E-45  to  3.402823        E38   for pos values
  double     8          -1.79769313486232E308  to -4.94065645841247E-324 for neg values
                         4.94065645841247E-324 to  1.79769313486232E308  for pos values

  string   1 per char      0 to approx 65,500 bytes
  variant  As appropriate  numeric to range of a Double or any character text


* Use a timer to test alternative code when optimizing a program.  Sometimes it
  may be necessary to put the code in a loop to get enough interations of the
  code in order to get a meaningful time value from the timer.  However, code
  that needs a loop just to get a good time value probably doesn't need any
  optimization!

  Example:

   start! = Timer
   For testit = 1 To 100

    F% = Len(text1)
    theFV! = Val(text2) * (1   interest) ^ F%
    text3 = Format$(theFV!, "0.00")
   
   Next
   MsgBox Str$(Timer - start!)

* Rather than repeatly access a property value in loop, assign the property
  to a variable prior to the loop execution.  Use the variable in the loop and
  then update any properties when the loop terminates.  This strategy is many
  times faster than using property values in a loop.

* Optimize the apparent speed of the application.  The apparent speed is how
  fast the application appears to be running to the end user.   The actual speed
  may actually be slower when using some of these methods.  The trick is to 
  make sure that the user can see something is going on, rather than long pauses.  
  Ideas include:
   - use the Show method on form load
   - set the mousepointer to an hourglass during processing (screen.mousepointer=11)
   - use a splash screen
   - consider loading hidden form(s) - use this sparingly, as it consumes resources
   - use status gauges. to conserve resources the line method can be used with box
     fill (BF) to create a very nice gauge.

Return to Phil's Web Page

Created by: Philip