Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
 
 
 
500MB 27GB Web Hosting - $9.95/Month
 
 
 
 
 
Old Pages
 
Old Index
Site Map
What's New
 
Books
How To
Tips & Tricks
Tutorials
Stories
Performance
Essays
Links
Q & A
New in VB6
Free Stuff
Pictures
 
 
 
TitleCompare the speed of passing variables ByRef and ByVal
KeywordsByVal, ByRef, speed, performance
CategoriesTips and Tricks
 
Make routines that pass variables ByRef and ByVal and see which is faster.

The differences are so tiny it's not really worth worrying about. You should use ByVal whenever you don't need to modify the value.

 
Private m_NumAssignments As Long

Private Sub ByteByRef(ByRef X As Byte)
Dim i As Long

    For i = 1 To m_NumAssignments
        X = 123
    Next i
End Sub

Private Sub IntegerByRef(ByRef X As Integer)
Dim i As Long

    For i = 1 To m_NumAssignments
        X = 123
    Next i
End Sub

Private Sub ByteByVal(ByVal X As Byte)
Dim i As Long

    For i = 1 To m_NumAssignments
        X = 123
    Next i
End Sub

Private Sub IntegerByVal(ByVal X As Integer)
Dim i As Long

    For i = 1 To m_NumAssignments
        X = 123
    Next i
End Sub

Private Sub cmdGo_Click()
Dim num_trials As Long
Dim start_time As Single
Dim stop_time As Single
Dim i As Long
Dim value_integer As Integer
Dim value_byte As Byte

    lblIntegerByRef.Caption = ""
    lblIntegerByVal.Caption = ""
    lblByteByRef.Caption = ""
    lblByteByVal.Caption = ""
    Screen.MousePointer = vbHourglass
    DoEvents

    ' Integers.
    num_trials = CLng(txtIterations.Text)
    m_NumAssignments = CLng(txtAssignments.Text)
    start_time = Timer
    For i = 1 To num_trials
        IntegerByRef value_integer
    Next i
    stop_time = Timer
    lblIntegerByRef.Caption = Format$(stop_time - _
        start_time, "0.00")
    DoEvents

    start_time = Timer
    For i = 1 To num_trials
        IntegerByVal value_integer
    Next i
    stop_time = Timer
    lblIntegerByVal.Caption = Format$(stop_time - _
        start_time, "0.00")
    DoEvents

    ' Bytes.
    start_time = Timer
    For i = 1 To num_trials
        ByteByRef value_byte
    Next i
    stop_time = Timer
    lblByteByRef.Caption = Format$(stop_time - start_time, _
        "0.00")
    DoEvents

    start_time = Timer
    For i = 1 To num_trials
        ByteByVal value_byte
    Next i
    stop_time = Timer
    lblByteByVal.Caption = Format$(stop_time - start_time, _
        "0.00")
    DoEvents

    Screen.MousePointer = vbDefault
End Sub
 
 
Copyright © 1997-2003 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated