Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleCompare the speeds of Integers and Longs
KeywordsInteger, Long, data type, speed, performance
CategoriesSoftware Engineering, Tips and Tricks
 
Click the Long button to make this program execute the following code to time some calculations using Longs. Click the Integer button to make it execute identical code with variables declared as Integers.
 
Private Sub cmdLong_Click()
Dim start_time As Single
Dim stop_time As Single

Dim num_trials As Long
Dim loop_num As Long
Dim i As Long
Dim j As Long
Dim k As Long

    lblLongResult.Caption = ""
    DoEvents
    start_time = Timer

    num_trials = CLng(txtTrials.Text)
    For i = 1 To num_trials
        For loop_num = 1 To 30000
            j = i \ 3
            k = j \ 3
            j = 2 * j + k
            If j > i Then Stop
            j = i \ 3
            k = j \ 2
            j = 2 * j + k
            If j > i Then Stop
            j = i \ 5
            k = j \ 5
            j = 2 * j + k
            If j > i Then Stop
        Next loop_num
    Next i

    stop_time = Timer
    lblLongResult.Caption = Format$(stop_time - start_time, _
        "0.00") & " seconds"
End Sub
 
In one test with 10 trials, the Integer calculations took 0.60 seconds and the Long calculations took 0.50 seconds.

When compiled and running 200 trials, the executable took about 0.68 seconds for the Integer calculations and 0.44 seconds for the Long calculations.

When compiled with all advanced optimizations (Project\Properties\Compile\Advanced Optimizations) and running 200 trials, the executable took about 0.55 seconds for the Integer calculations and 0.38 seconds for the Long calculations.

The morale: using a data type that is natural for your computer will produce faster results. On most systems, that will be Longs rather than Integers.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated